面向对象的Tkinter功能 - 我将如何把这个?(Object Orientated Tkinte

2019-09-29 19:30发布

我编码一个计划,将需要的功能改变标签和文本输入到文本框,但是我不知道如何与节目的新风格,我使用(面向对象),它这么做。 我已经做了,但是我生成使用此代码的帧之前的程序:

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级不同的帧。 我怎样才能在一个帧获得按钮以使标签说“你好”的框架被压后?

Answer 1:

我已经修改了你的网页类之一来说明它如何做。 它涉及添加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')

截图

之前按下按钮:

后按下按钮:



文章来源: Object Orientated Tkinter Functions - How would I put this in?