홈>
오른쪽 화살표 또는 왼쪽 화살표 키를 눌렀을 때 다음 이미지와 이전 이미지를 표시하는 방법은 무엇입니까? 나는
Example
를 정의하지 않을 때 간단한 예 에서이 작업을 수행했습니다.
아래 예제 코드에서 클래스를 사용하려고 할 때 실패했습니다.
Example
내부에 두 개의 정적 메소드를 정의했습니다.
왼쪽 화살표와 오른쪽 화살표를 GUI에 바인딩하는 클래스입니다. 이벤트 인쇄에 잘 작동하지만
image
를 업데이트하는 방법을 모르겠습니다
configure(image = next_photo)
의 라벨 옵션
그리고
configure(image = previous_photo)
.
내 코드의 일부는 다음과 같습니다.
current = 0
class Example(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.master.bind('<Left>', self.left_key)
self.master.bind('<Right>', self.right_key)
image = Image.open(image_list[0])
image = image.resize((800, 600),Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
self.logo = tk.Label(self, text="Logo", background="orange")
self.logo2 = tk.Label(self, image = photo)
############################################# ScrolledText ##########################################
self.edit_space = tkst.ScrolledText(
master = self,
wrap = 'word', # wrap text at full words only
width = 50, # characters
height = 10, # text lines
bg='beige' # background color of edit area
)
self.other1 = tk.Label(self, background="purple")
self.other2 = tk.Label(self, background="yellow")
self.other3 = tk.Label(self, background="pink")
self.other4 = tk.Label(self, background="gray")
self.main = tk.Frame(self, background="blue")
self.logo.grid(row=0, column=0, rowspan=1, sticky="nsew")
self.logo2.grid(row=1, column=0, rowspan=1, sticky="nsew")
self.other1.grid(row=0, column=1, sticky="nsew")
self.other2.grid(row=0, column=2, sticky="nsew")
self.other3.grid(row=1, column=1, sticky="nsew")
self.other4.grid(row=1, column=2, sticky="nsew")
self.edit_space.grid(row=2, column=0, sticky="nsew")
self.main.grid(row=2, column=2, columnspan=2, rowspan=6)
for row in range(8):
self.grid_rowconfigure(row, weight=1)
for col in range(3):
self.grid_columnconfigure(col, weight=1)
@staticmethod
def left_key(event):
print(str(event) + " key pressed")
global current, image_list
if not (0 <= current + 1 < len(image_list)):
print("This is the first image")
return
current += 1
image = Image.open(image_list[current])
image = image.resize((800, 600),Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
# self.logo2 = tk.Label(self, image = photo)
#self.logo2.configure(image = photo)
@staticmethod
def right_key(event):
print(str(event) + " key pressed")
global current, image_list
if not (0 <= current - 1 < len(image_list)):
print("This is the last image")
return
current -= 1
image = Image.open(image_list[current])
image = image.resize((800, 600),Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
# self.logo2 = tk.Label(self, image = photo)
#self.logo2.configure(image = photo)
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.geometry("800x400")
root.mainloop()
- 답변 # 1
관련 자료
- winforms - C # 양식 클래스 이벤트
- python - tkinter - stringvar를 다른 클래스에 전달하는 방법은 무엇입니까?
- python 3.x - 스레딩을 사용하여 tkinter에서 이미지로드시 'pyimage2'가 존재하지 않음을 보여줍니다
- python - tkinter 라디오 버튼 - osx에서 이미지가 픽셀 화되었지만 ubuntu에서는 잘 작동합니다
- Android 411에서 Kotlin을 사용하여 모든 클래스의 활동 뷰 값을 업데이트하는 가장 좋은 방법
- python - 클래스에서 tk를 사용할 때 tkinter destroy ()가 작동하지 않습니다
- python - Canvas를 사용하여 이미지를 가져가는 방법은 무엇입니까? TKinter
- python - Tkinter에서 텍스트 레이블을 업데이트하는 방법은 무엇입니까?
- django rest api에서 업데이트 이미지가 작동하지 않습니다
- javascript - 이벤트 리스너를 통해 이미지 전환/표시
- Python tkinter가 다른 레이어로 이미지를 생성합니까?
- python - 텍스트 파일에서 읽을 때 Tkinter 레이블을 업데이트하는 방법
- Java 클래스의 데이터베이스에서 작업 중이며 데이터베이스의 특정 값/열을 업데이트하는 방법을 알아내는 데 도움이 필요합니다
- python - 클래스가 호출 될 때 실행되는 모든 함수 (tkinter)
- html - 내 ngx-gallery-9 배열의 첫 번째 이미지에 CSS 클래스 적용
- python - 빈 목록에서 Tkinter ComboBox를 동적으로 업데이트
- python - tkinter의 한 클래스 함수에서 값을 얻는 방법
- HTML CSS - html css - css를 사용하여 이미지로 클래스를 정의 할 수 있습니까?
- python - 이미지에 애니메이션을 제공 할 수 있습니까? - tkinter
- Google Analytics 이벤트 이미지 태그 생성
관련 질문
- python : ImageTk가 정의 된 후 회전하는 방법이 있습니까?
- python : .wait_variable (var)로 인해 GUI가 정지됨
- python : 탐색기 팝업없이 무한 루프에서 멈춤
- Python Tkinter는 askopenfilenames ()의 결과를 얻습니다.
- python : django.db.utils.ProgrammingError : makemigrations를 실행할 때 "company_company"관계가 존재하지 않습니다.
- python : 우분투 18.04에서 pyqt5 설치 /가져 오기 어려움
- python : 2 개의 가변 범위
- Python Selenium은 특정 div 클래스를 찾고, 다른 div가 내부에있는 경우 세 번째 div를 찾아 텍스트를 복사합니다.
- python : 가져온 패키지에서 작업 디렉토리 및 파일에 동일한 이름을 가진 파일로 인해 ImportError가 발생했습니다.
- python : 여러 csv 파일을 HDF5로 읽을 때 Pandas ParserError EOF 문자
주된 문제는 지역 변수
photo
를 생성한다는 것입니다__init__()
내부 그것을logo2
의 이미지로 사용하십시오. . 그러나photo
__init__()
후에 파괴 될 것입니다 이미지가 표시되지 않도록 실행됩니다.문제를 해결하려면 :
self.photo
사용photo
대신2 개의 정적 메소드
left_key()
변경 그리고right_key()
인스턴스 메소드로logo2
의 이미지를 설정left_key()
내부 그리고right_key()
:self.logo2.configure(image=self.photo)
current
의 가치를 확인left_key()
내부 그리고right_key()
문제가 발생할 수 있습니다 :left_key()
의current > 0
확인해야합니다right_key()
의current < len(image_list)-1
확인해야합니다와
current
의 조정 이 두 가지 방법에서 잘못된 방향입니다 :left_key()
current
를 줄여야한다right_key()
current
를 증가시켜야한다