面向对象的 Tkinter 函数 - 我如何将其放入? [英] Object Orientated Tkinter Functions - How would I put this in?

查看:30
本文介绍了面向对象的 Tkinter 函数 - 我如何将其放入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am coding a program which will need functions to change labels and enter text into text boxes however I do not know how to do this with a new style of programming which I am using (Object Orientated). I have done the program before however I generated the frames using this code:

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

然后我把它全部放在一个我跑过的班级里,但那是糟糕的形式,所以我正在重做.到目前为止,我的代码如下:

and then I put it all in one class which I ran however that was bad form so I am redoing it. My code so far is as follows:

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 个不同的框架.如何在框架中获得一个按钮,使标签在按下后在框架中说你好"?

If you try it out it works however it just has 3 different frames. How can I get a button in a frame to make a label say "Hello" in the frame after it is pressed?

推荐答案

我修改了您的一个页面类来说明如何实现.它包括添加一个 Label 来保存消息,一个 Button 来控制它,以及一个简单地称为 handler() 的函数,当后者被按下.它通过使小部件成为包含 Frame 子类实例 self 的属性来保存小部件,因此它们可以在 handler() 函数中轻松引用(不诉诸全局变量).

I've modified one of your page classes to illustrate how it could be done. It involved adding a Label to hold the message, a Button to control it, and a function, called simply handler(), to call when the latter is pressed. It saves the widgets by making them attributes of the containing Frame subclass instance, self, so they can be easily referenced in the handler() function (without resorting to global variables).

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

截图

按钮被按下之前:

Screenshots

Before button is pressed:

按下按钮后:

这篇关于面向对象的 Tkinter 函数 - 我如何将其放入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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