gpt4 book ai didi

python - 面向对象的 Tkinter 函数 - 我如何将其放入?

转载 作者:太空宇宙 更新时间:2023-11-03 15:48:26 27 4
gpt4 key购买 nike

我正在编写一个程序,该程序需要更改标签并将文本输入到文本框中的功能,但是我不知道如何使用我正在使用的新编程风格(面向对象)来做到这一点。我之前已经完成了该程序,但是我使用以下代码生成了框架:

f = [Frame(root) for i in range(0,5)]
for i in f:
i.place(relx=0,rely=0,relwidth=1,relheight=1)

然后我把它全部放在我运行的一个类中,但是这是不好的形式,所以我正在重做它。到目前为止我的代码如下:

import tkinter as tk
import datetime,time,os,sys
import sqlite3 as lite
from datetime import date

Title_Font= ("Times", 18, "underline italic")

unix = time.time()
time_curr = str(datetime.datetime.fromtimestamp(unix).strftime('%H:%M'))
date1 = str(datetime.datetime.fromtimestamp(unix).strftime('%d-%m-%Y'))


class Creating_Stuff(tk.Tk):

def __init__(self, *args, **kwargs):

tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)


container.pack(side="top", fill="both", expand = True)

container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)

self.frames = {}

for F in (StartPage, PageOne, PageTwo):

frame = F(container, self)

self.frames[F] = frame

frame.grid(row=0, column=0, sticky="nsew")

self.show_frame(StartPage)

def show_frame(self, cont):

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

class StartPage(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(3, weight=1)

self.option_add( "*font", "Times 12" )
self.tk_setPalette(background='#bbfff0', foreground='black',
activeBackground='#d9d9d9', activeForeground='#ff9933')


label = tk.Label(self, text="Laptop Booking System", font=Title_Font)
label.grid(row=0, column=1, pady = 30)

time = tk.Label(self, text="Time: " + time_curr + "\nDate: " + date1, font="Times 10")
time.grid(row=0, column=2,columnspan=2)

Booking_1 = tk.Button(self, text="Booking",
command=lambda: controller.show_frame(PageOne),bg='#f2f2f2',width=20)
Booking_1.grid(row=2, column=1, pady=10)

Tbl_q1 = tk.Button(self, text="Table Querying",
command=lambda: controller.show_frame(PageTwo),bg='#f2f2f2',width=20)
Tbl_q1.grid(row=3, column=1, pady=10)

Exit = tk.Button(self, text ="Exit",command=lambda:destroy(),bg='#f2f2f2')
Exit.grid(row=5, column=1)


class PageOne(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(3, weight=1)
label = tk.Label(self, text="Page One!!!", font=Title_Font)
label.grid(row=1, column=1)


bk2_menu = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
bk2_menu.grid(row=3, column=1)



class PageTwo(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(3, weight=1)
label = tk.Label(self, text="Page Two!!!", font=Title_Font)
label.grid(row=1, column=1)


bk2_Menu2 = tk.Button(self, text="Back to menu",
command=lambda: controller.show_frame(StartPage))
bk2_Menu2.grid(row=3, column=1)



app = Creating_Stuff()
def destroy():
app.destroy()

app.title("Laptop Booking System")
app.geometry("700x400")
app.mainloop()

如果你尝试一下,它可以工作,但它只有 3 个不同的框架。如何在框架中获取按钮,使标签在按下后在框架中显示“Hello”?

最佳答案

我修改了您的一个页面类来说明它是如何完成的。它涉及添加一个 Label 来保存消息、一个 Button 来控制它,以及一个简单地称为 handler() 的函数,用于在以下情况下调用:后者被按下。它通过将小部件设为包含 Frame 子类实例 self 的属性来保存小部件,以便可以在 handler() 函数中轻松引用它们(不求助于全局变量)。

class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(3, weight=1)
label = tk.Label(self, text="Page One!!!", font=Title_Font)
label.grid(row=1, column=1)

self.msg_label = tk.Label(self, text="")
self.msg_label.grid(row=2, column=1)
self.msg_button = tk.Button(self, text='Show Message',
command=self.handler)
self.msg_button.grid(row=3, column=1)
self.msg_toggle = False

bk2_menu = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
bk2_menu.grid(row=5, column=1)

def handler(self):
self.msg_toggle = not self.msg_toggle
if self.msg_toggle:
self.msg_label.config(text='Hello')
self.msg_button.config(text='Clear Message')
else:
self.msg_label.config(text='')
self.msg_button.config(text='Show Message')

屏幕截图

按下按钮之前:

screenshot showing before button is pressed

按下按钮后:

screenshot showing before button is pressed

关于python - 面向对象的 Tkinter 函数 - 我如何将其放入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41557785/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com