多个框架,多个按钮和标签与变化的图像,Tkinter



我正在用Tkinter设计一个GUI。它有许多帧(页面(,通过按下一帧中的按钮,该帧将被销毁,并显示下一帧。每个按钮都有可变的图像,所以我需要一个功能来旋转显示的每个页面的按钮图像。

我写了下面的代码,照片的地址发生了变化(Pagestart类的(def counter(中的self.Address(,但我认为button.config无法更新按钮的图像!!!为什么?

(用于解释:主类showframe函数中的countercounter函数负责更新pagestart中的计数器函数。(

这段代码的输出显示了一个带有一个按钮的框架,它的图像是恒定的,不能更新。

import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
import time
import os
import subprocess as sp
import signal
global counter0, counter1
counter0=0
counter1=0

class Project(tk.Tk):
def __init__(self, *args, **kwargs):

tk.Tk.__init__(self, *args, **kwargs)      

container = tk.Frame(self)
container.configure(background="#000000")
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.container=container
self.frames = {}
for F in ( Pagestart, PageOne):   
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")             
self.show_frame(Pagestart)

def show_frame(self, cont):
self.sw = 1000
self.sh = 1800
self.cont=cont
for frame in self.frames.values():
frame.grid_remove()
frame = self.frames[cont]
frame.configure(background="#000000")      
frame.grid()
frame.winfo_toplevel().geometry('%dx%d+%d+%d' % (self.sw,self.sh,0,0))

A=Pagestart(parent=self.container, controller=self)
self.Pagestart=Pagestart
B=A.button

def countercounter(B):        
def count1():
global counter0, counter1
A.counter()
if (self.cont==Pagestart):
B.after(100,count1)
count1()
countercounter(B)


def twoside(self, inputaddress, startframe, stopframe):
self.input = inputaddress
self.startframe = startframe
self.stopframe = stopframe       
global counter0, counter1
def count():  
global counter0, counter1
if (counter1==1):
counter0 -=1
if (counter1==0):
counter0 += 1
self.Address=('%s%s' % (str(self.input),str(counter0))+".jpg")
if (counter0==self.stopframe):
counter1=1
if (counter0==self.startframe):
counter1=0
count()

def sendAddress(self):
return self.Address  


class Pagestart(tk.Frame):

def __init__(self, parent, controller):

tk.Frame.__init__(self, parent)
self.controller = controller   
self.ButtonStyle = ttk.Style()
self.ButtonStyle.configure("Tabedstart.TButton", background="#000000", borderwidth=0)
self.ButtonStyle.map("Tabedstart.TButton", background=[('selected', "#000000")])
self.button = ttk.Button(self, style="Tabedstart.TButton", command=lambda: controller.show_frame(PageOne))
self.button.pack(pady=320)     
self.counter()      

def counter(self):

self.inputaddress = "/home/pi/Documents/Reference0/"
self.controller.twoside(self.inputaddress, 0, 138)
self.Address = self.controller.sendAddress()
self.photo = Image.open(self.Address)
self.photo = ImageTk.PhotoImage(self.photo)       
self.button.image=self.photo
self.button.config(image=self.photo)

class PageOne(tk.Frame):

def __init__(self, parent, controller):

tk.Frame.__init__(self, parent)
self.controller = controller   
self.ButtonStyle = ttk.Style()
self.ButtonStyle.configure("Tabedstart.TButton", background="#000000", borderwidth=0)
self.ButtonStyle.map("Tabedstart.TButton", background=[('selected', "#000000")])
self.button = ttk.Button(self, style="Tabedstart.TButton", command=lambda: controller.show_frame(Pagestart))
self.button.pack(pady=320)     
self.counter()      

def counter(self):

self.inputaddress = "/home/pi/Documents/Reference1/"
self.controller.twoside(self.inputaddress, 0, 138)
self.Address = self.controller.sendAddress()
self.photo = Image.open(self.Address)
self.photo = ImageTk.PhotoImage(self.photo)       
self.button.image=self.photo
self.button.config(image=self.photo)

if __name__ == "__main__":
app = Project()
app.mainloop()

您不销毁帧,但只使用grid_forget()/grid_remove()隐藏它。

不要创建Pagestart的新实例,因为您已经有用显示的Pagestart的旧实例

frame = self.frames[cont]
frame.grid()

在这种情况下,您应该使用ie.更改图像

frame.counter()

工作代码:

我使用self.counterself.animation_direction而不是全局变量counter0counter1

我不使用嵌套函数,因为它可读性较差。

我使用change_image()每100ms 更改一次图像

import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk

class Project(tk.Tk):
def __init__(self, *args, **kwargs):

tk.Tk.__init__(self, *args, **kwargs)      

self.counter = 1
self.animation_direction = 1  # it will add `+1` to self.counter
self.sw = 1000
self.sh = 1800

container = tk.Frame(self)
container.configure(background="#000000")
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.container = container

self.frames = {}

for F in ( PageStart, PageOne):   
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")

self.show_frame(PageStart)
def show_frame(self, cont):
self.cont = cont
for frame in self.frames.values():
frame.grid_remove()

frame = self.frames[cont]
frame.configure(background="#000000")      
frame.grid()
frame.winfo_toplevel().geometry('%dx%d+%d+%d' % (self.sw,self.sh,0,0))

#frame.counter()
self.change_image()

def twoside(self, inputaddress, startframe, stopframe):
self.input = inputaddress
self.startframe = startframe
self.stopframe = stopframe       

self.counter += self.animation_direction
self.address = '%s%s.jpg' % (self.input, self.counter)

if self.counter == self.stopframe:
self.animation_direction = -self.animation_direction
if self.counter == self.startframe:
self.animation_direction = -self.animation_direction

def get_address(self):
return self.address  

def change_image(self):
if self.cont == PageStart:
self.frames[self.cont].counter()
self.after(100, self.change_image)


class PageStart(tk.Frame):  # PEP8: UpperCaseNames for classes

def __init__(self, parent, controller):

tk.Frame.__init__(self, parent)
self.controller = controller

self.ButtonStyle = ttk.Style()
self.ButtonStyle.configure("Tabedstart.TButton", background="#000000", borderwidth=0)
self.ButtonStyle.map("Tabedstart.TButton", background=[('selected', "#000000")])

self.button = ttk.Button(self, style="Tabedstart.TButton", command=lambda: controller.show_frame(PageOne))
self.button.pack(pady=320)     

self.counter()      

def counter(self):

self.inputaddress = "/home/pi/Documents/Reference0/"
self.controller.twoside(self.inputaddress, 0, 138)

self.address = self.controller.get_address()  # PEP8: lower_case_names for functions/methods and variables
self.photo = Image.open(self.address)
self.photo = ImageTk.PhotoImage(self.photo)

self.button.image = self.photo
self.button.config(image=self.photo)

class PageOne(tk.Frame):

def __init__(self, parent, controller):

tk.Frame.__init__(self, parent)
self.controller = controller

self.ButtonStyle = ttk.Style()
self.ButtonStyle.configure("Tabedstart.TButton", background="#000000", borderwidth=0)
self.ButtonStyle.map("Tabedstart.TButton", background=[('selected', "#000000")])

self.button = ttk.Button(self, style="Tabedstart.TButton", command=lambda: controller.show_frame(PageStart))
self.button.pack(pady=320)

self.counter()      

def counter(self):

self.inputaddress = "/home/pi/Documents/Reference1/"
self.controller.twoside(self.inputaddress, 0, 138)

self.address = self.controller.get_address()
self.photo = Image.open(self.address)
self.photo = ImageTk.PhotoImage(self.photo)

self.button.image = self.photo
self.button.config(image=self.photo)

if __name__ == "__main__":
app = Project()
app.mainloop()

最新更新