这是我的第一篇文章,我仍然是python世界的新手。我试图用python / tkinter创建一个GUI应用程序,我遇到了一些麻烦。我有2个图像文件夹,我需要显示一个带有一些文本的页面,然后显示文件夹A中的图像,然后显示带有文本的同一页面,然后显示文件夹B中的图像。我可以将这些图像显示出来起来,但我的问题是保持一切中心,看起来很好。如果我调整小于父框架的图像大小,则边缘周围会有空白。由于窗口小部件的小小,页面边缘周围有极端的空白区域。
以下列出了我遇到的问题:
以下是我对这些问题的框架所拥有的代码。我知道这不是最干净的,但如果可能的话,我真的很感激帮助。
class TrialPage(tk.Frame):
def __init__(self, parent,controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.parent = parent
self.configure(bg='#5B9BD5')
self.stimFrames = []
self.evalList = []
#initialize object to retrieve image paths
self.trialData = TrialData()
self.trialStim = [self.trialData.Trial_Stimuli_1,
self.trialData.Trial_Stimuli_2,
self.trialData.Trial_Stimuli_3,
self.trialData.Trial_Stimuli_4,
self.trialData.Trial_Stimuli_5,
self.trialData.Trial_Stimuli_6,
self.trialData.Trial_Stimuli_7,
self.trialData.Trial_Stimuli_8,
self.trialData.Trial_Stimuli_9,
self.trialData.Trial_Stimuli_10,
self.trialData.Trial_Stimuli_11]
self.trialControl = [self.trialData.Trial_Control_1,
self.trialData.Trial_Control_2,
self.trialData.Trial_Control_3,
self.trialData.Trial_Control_4,
self.trialData.Trial_Control_5,
self.trialData.Trial_Control_6,
self.trialData.Trial_Control_7,
self.trialData.Trial_Control_8,
self.trialData.Trial_Control_9,
self.trialData.Trial_Control_10,
self.trialData.Trial_Control_11]
self.picture_display = tk.Label(self, bg="#5B9BD5")
self.picture_display.grid(row=1,column=1,sticky="nsew")
#Get size of screen
self.imageWidth = int(parent.winfo_width())
self.imageHeight = int(parent.winfo_height())
#cycle through images. resize was attempted but not implemented
self.pictures = cycle((imageTk.PhotoImage(image.open(imagePath).resize((int(self.imageWidth),int(self.imageHeight)),image.ANTIALIAS)))for imagePath in self.trialStim)
'''Center the image display on the frame DOESN'T WORK'''
self.grid_rowconfigure(0,weight=1)
self.grid_rowconfigure(2,weight=1)
self.grid_columnconfigure(0,weight=1)
self.grid_columnconfigure(2,weight=1)
self.show_break()
def show_slides(self):
#cycle through the images and show them
img_object = next(self.pictures)
#display image
self.picture_display.config(image=img_object)
#wait 2 seconds and display break page
self.after(2000, self.show_break)
def show_break(self):
#hide image
self.picture_display.config(image='')
#display text
self.picture_display.config(fg="#FFFFFF",bg='#5B9BD5',font=('Arial',18),text="Please relax and take a break. The next slide will begin shortly.")
#wait 2 seconds and display break page
self.after(2000, self.show_slides)