如何更新 LabelFrame 标题 (Tkinter) [英] How to update LabelFrame title (Tkinter)

查看:28
本文介绍了如何更新 LabelFrame 标题 (Tkinter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,第一次使用 Tkinter.我的项目是一本我间谍"书,它使用按钮将图片移动到图片.我喜欢 LabelFrame 小部件的外观,并希望为与每个图像对应的文本使用标题.我能够正确更新图像,但标题保持不变.我试过配置,忘记然后重建框架,我想到了一些我不记得的东西,但这些都没有奏效.我在网上搜索过,回顾了 Stack Overflow 类似的问题——这些问题很少,让我相信这是不可能的.感谢您的帮助.

I am a novice and working with Tkinter for the first time. My project is an "I Spy" book which moves picture to picture utilizing buttons. I like the look of the LabelFrame widget and want to use the title for the text that corresponds with each image. I am able to achieve the images updating correctly but the title remains the same. I have tried config, forgetting and then rebuilding the frame, and I think something else which I can't recall and none of that has worked. I have searched online, reviewed Stack Overflow similar questions--which are very few and lead me to believe that this cannot be done. Thank you for your assistance.

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()

推荐答案

如果您对示例有任何疑问,我会尽力为您解答.

If you have some questions about the exampel I'll try to answer you.

    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()

希望有帮助.

这篇关于如何更新 LabelFrame 标题 (Tkinter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆