簡體   English   中英

如何更新 LabelFrame 標題(Tkinter)

[英]How to update LabelFrame title (Tkinter)

我是新手,第一次使用 Tkinter。 我的項目是一本“I Spy”書,它使用按鈕將圖片移動到圖片。 我喜歡 LabelFrame 小部件的外觀,並希望將標題用於與每個圖像對應的文本。 我能夠正確更新圖像,但標題保持不變。 我嘗試過配置,忘記然后重建框架,我想其他一些我不記得的東西,但都沒有奏效。 我在網上搜索過,查看了 Stack Overflow 類似的問題——這些問題很少,讓我相信這是不可能的。 謝謝您的幫助。

from tkinter import *
from tkinter import Button
from tkinter import Label
from PIL import ImageTk
from PIL import Image

root = Tk()
root.title('')
root.attributes('-toolwindow', True)
root.geometry('620x660+100+0')


img2 = ImageTk.PhotoImage(Image.open('spy_images/rainbow.jpg'))
img4 = ImageTk.PhotoImage(Image.open('spy_images/pods.jpg'))
img5 = ImageTk.PhotoImage(Image.open('spy_images/lion.jpg'))
img6 = ImageTk.PhotoImage(Image.open('spy_images/bike.jpg'))
img7 = ImageTk.PhotoImage(Image.open('spy_images/binary.jpg'))

image_list = [img2, img4, img5, img6, img7]

text2 = 'A rainbow, not in the sky!'
text4 = 'Dangly, weird seed pods.'
text5 = 'A stoney grin.'
text6 = 'A lane just for bikes.'
text7 = 'A different way to count.'

text_list = [text2, text4, text5, text6, text7]

make_frame = LabelFrame(root, text='A rainbow, not in the sky!', width=100, height=100,
                        font=('Arial', 14, 'bold'), fg='red', bd=10)
make_frame.grid(row=0, column=1, columnspan=5)
img_filename = 'spy_images/rainbow.jpg'
PIL_image = Image.open(img_filename)

img = ImageTk.PhotoImage(PIL_image)
in_frame = Label(make_frame, image=img)
in_frame.grid(padx=10, pady=10)


def forward(image_num, text_num):
    global make_frame
    global in_frame
    global button_forward
    global button_back

    in_frame.grid_forget()

    in_frame = Label(image=image_list[image_num])
    button_forward = Button(root, text='>>', command=lambda:
                            forward(image_num+1, text_num+1))
    button_back = Button(root, text='<<', command=lambda:
                         back(image_num-1, text_num-1))

    if image_num == 7:
        button_forward = Button(root, text='>>', state=DISABLED)

    make_frame.grid(row=0, column=1, columnspan=5)

    in_frame.grid(row=0, column=0, columnspan=5)
    in_frame.grid(padx=10, pady=10)

    button_forward.grid(row=1, column=5)
    button_back.grid(row=1, column=1)
    button_back.grid_columnconfigure(0, weight=1)
    button_back.grid_columnconfigure(2, weight=1)
    button_back.grid_columnconfigure(4, weight=1)


def back(image_num, text_num):
    global make_frame
    global in_frame
    global button_forward
    global button_back

    in_frame.grid_forget()

    in_frame = Label(image=image_list[image_num - 1])
    button_forward = Button(root, text='>>', command=lambda:
                            forward(image_num + 1, text_num + 1))
    button_back = Button(root, text='<<', command=lambda:
                         back(image_num - 1, text_num - 1))

    if image_num == 1:
        button_back = Button(root, text='<<', state=DISABLED)

    make_frame.grid(row=0, column=1, columnspan=5)

    in_frame.grid(row=0, column=0, columnspan=3)
    in_frame.grid(padx=10, pady=10)

    button_forward.grid(row=1, column=5)
    button_back.grid(row=1, column=1)
    button_back.grid_columnconfigure(0, weight=1)
    button_back.grid_columnconfigure(2, weight=1)
    button_back.grid_columnconfigure(4, weight=1)


button_back = Button(root, text='<<', command=back, state=DISABLED, bg='#d9d5d4',
                     font=('Arial', 14, 'bold'))
button_exit = Button(root, text='Cancel', command=root.quit, bg='#d9d5d4', font=('Arial', 12))
button_forward = Button(root, text='>>', command=lambda: forward(2, 2), bg='#d9d5d4', font=('Arial', 14, 'bold'))

button_back.grid(row=1, column=1)
button_exit.grid(row=1, column=3)
button_forward.grid(row=1, column=5)

button_back.grid_columnconfigure(0, weight=1)
button_back.grid_columnconfigure(2, weight=1)
button_back.grid_columnconfigure(4, weight=1)

root.mainloop()

如果您對示例有任何疑問,我會盡力回答您。

    import tkinter as tk
#you dont need this but if you want to cycle, wich is would be nice there you go
from itertools import cycle

#create a list with strings you like to display
li = ['A rainbow, not in the sky!','Dangly, weird seed pods.','A stoney grin.',
      'A lane just for bikes.','A different way to count.']
#here we create a cycle of that list
my_cycled_li = cycle(li)

#the change function
def change():
  #set var to next element in list
  var.set(next(my_cycled_li))
  #update the LabelFrame
  lf.configure(text=var.get())

root = tk.Tk()
#variable to change
var = tk.StringVar()
#there can be a default setting
var.set('default')
lf = tk.LabelFrame(root,text=var.get(),width=200,height=100,bg='red')
#you dont need this, this means the Frame size isnt the size of the widget it contains.
lf.pack_propagate(0)
lf.pack()
b = tk.Button(lf,text='change', command=change)
b.pack()


root.mainloop()

希望它有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM