这是 tkinter 中的不良编程习惯吗? [英] Is this bad programming practice in tkinter?

查看:63
本文介绍了这是 tkinter 中的不良编程习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用 tkinter 编写事件驱动程序,并且正在使用 Fredrik Lundh 的优秀教程.在那,他提到最好为框架定义一个类(应用程序)并将程序作为类的实例运行,而不是像这样启动它:

I'm learning to write event driven programs using tkinter and am using Fredrik Lundh's excellent tutorial. In that, he mentions that it's better to define a class (App) for the frame and run the program as an instance of the class instead of just starting it up thus:

root = Tk()
w = Label(root, text = 'hello, world!')
w.pack()
root.mainloop()

我有 3 个问题:

  1. 以这种更简单的方式进行编程是不是很糟糕?

  1. Is it bad programming practice to do it in this simpler way?

如果我确实定义了一个类,并将回调函数绑定到小部件,那么这些函数是否都必须在类本身中?即,我可以在课程中设置一个显示 go 的按钮,当我点击该按钮时,它会运行一个在课程外定义的精心设计的例程吗?

If I do define a class, with callback functions bound to the widgets, do the functions all have to be within the class itself? i.e. Can I have a button that says go within the class, which when I click runs an elaborate routine that's been defined outside the class?

然后将结果生成在类外并在类内显示是不好的做法吗?

Is it bad practice to then take the result generate outside the class and display it within?

本质上,我可以通过跳入和跳出类来使程序运行,但我不确定这样做是否是不好的做法(如全局变量).

Essentially, I can make the program work by jumping in and out of the class but am not sure if it's bad practice to do so (a la global variables).

推荐答案

  1. 随着您的应用程序变大,您将需要使用类.您可以一次专注于一节课,而不必将注意力集中在整个代码上.
  2. 您不仅限于使用类中的方法.您的代码可能会利用外部函数或类来获取信息,甚至修改提供给它们的参数.
  3. 不,这可能是显示信息的方式.或者,您可以使用一个文件来输出您的结果,如果它存在,则可以打印到控制台.

示例:

import tkinter
import random

class Application(tkinter.Frame):

    @classmethod
    def main(cls):
        root = tkinter.Tk()
        frame = cls(root)
        frame.grid()
        root.mainloop()

    def __init__(self, master=None, cnf={}, **kw):
        super().__init__(master, cnf, **kw)
        self.w = tkinter.Label(self, text='Hello, world!')
        self.w.grid()
        self.v = tkinter.Button(self, text='Press Me', command=self.click)
        self.v.grid()
        self.u = tkinter.Button(self, text='Me Too!',
                                command=lambda: external_mutator(self.w))
        self.u.grid()

    def click(self):
        self.w['text'] = external_function(3)

def external_function(ndigits):
    return round(random.random(), ndigits)

def external_mutator(widget):
    widget['text'] = external_function(6)
    print('Hello to you too!')  # shown on console if present

if __name__ == '__main__':
    Application.main()

<小时>

main 类方法的替代方法:


Alternative to the main classmethod:

import tkinter
import random

class Main(tkinter.Tk):

    def __init__(self, screenName=None, baseName=None, className='Tk',
                 useTk=1, sync=0, use=None):
        super().__init__(screenName, baseName, className,
                         useTk, sync, use)
        frame = Application(self)
        frame.grid()
        self.mainloop()

class Application(tkinter.Frame):

    def __init__(self, master=None, cnf={}, **kw):
        super().__init__(master, cnf, **kw)
        self.w = tkinter.Label(self, text='Hello, world!')
        self.w.grid()
        self.v = tkinter.Button(self, text='Press Me', command=self.click)
        self.v.grid()
        self.u = tkinter.Button(self, text='Me Too!',
                                command=lambda: external_mutator(self.w))
        self.u.grid()

    def click(self):
        self.w['text'] = external_function(3)

def external_function(ndigits):
    return round(random.random(), ndigits)

def external_mutator(widget):
    widget['text'] = external_function(6)
    print('Hello to you too!')  # shown on console if present

if __name__ == '__main__':
    Main()

这篇关于这是 tkinter 中的不良编程习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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