Python tkinter作用域标签不显示

2024-05-29 02:55:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,我相信我现在遇到了一个范围问题。 我试图用函数调用弹出一个窗口。不过,我确实认为,通过上课可以更好地做到这一点。就像我说的,我是个初学者。这两种语法修复以及更好的编程方法都是受欢迎的。我的代码如下

from tkinter import *

def showProdScreen():
  root = Tk()
  root.title("Production")
  count = 0
  countStr = StringVar()

  countStr.set("0");

  def countUp():
    nonlocal count
    nonlocal countStr
    print(countStr.get())
    count = count + 1
    countStr.set(count)


  up = Button(root, text = "AddCnt", command = countUp, width = 15, font = ("Curier", 16))

  countl = Label(root, text = "Count", relief = RAISED, font = ("Curier", 20), width = 10)
  countVal = Label(root, textvariable = countStr, relief = RAISED, font = ("Curier", 20), width = 10)


  countVal.pack();
  countl.pack();
  up.pack()

提前谢谢


Tags: textdefcountrootwidthlabelpackfont
1条回答
网友
1楼 · 发布于 2024-05-29 02:55:23

好的,这是你应该做的事情的清单。你知道吗

  1. 在使用tkinter时,确实应该执行import tkinter as tk,然后使用tk.前缀。这将防止错误地过度使用内置方法。

  2. Python PEP8说每个缩进使用4个空格,所以开始使用4而不是2个空格。

  3. 我认为人们在函数中使用函数并不常见,我想这是有充分理由的。

  4. 我从未见过使用nonlocal(目前使用Python只有2年),所以这对我来说是新的。通常在处理tkinter中的非类函数时会使用global。注意,您可能应该使用类来避免global

  5. 跟踪计数器时,应使用数字变量之一。例如,这里我们可以使用IntVar(),然后不需要使用计数器跟踪。

下面是一些清理代码的示例,一个非OOP示例和一个类示例。你知道吗

您在更新的问题中所做的是一种构建GUI的奇怪方式。以下是您的示例:

from tkinter import *

def showProdScreen():
    root = Tk()
    root.title("Production")
    count = 0
    countStr = StringVar()
    countStr.set("0");

    def countUp():
        nonlocal count
        nonlocal countStr
        print(countStr.get())
        count = count + 1
        countStr.set(count)

    Button(root, text="AddCnt", command=countUp, width=15, font=("Curier", 16)).pack()
    Label(root, text="Count", relief=RAISED, font=("Curier", 20), width=10).pack()
    Label(root, textvariable=countStr, relief=RAISED, font=("Curier", 20), width=10).pack()

    root.mainloop()

showProdScreen()

也就是说,我不知道为什么要用函数内部的函数来构建它。你知道吗

当以非面向对象的方式构建时,应该在这里使用全局和函数。 对于非面向对象的示例,我将执行以下操作:

import tkinter as tk

root = tk.Tk()
root.title("Production")
count_str = tk.IntVar()
count_str.set(0)

def countUp():
    global count_str
    count_str.set(count_str.get() + 1)

tk.Label(root, textvariable=count_str, relief="raised", font=("Curier", 20), width=10).pack()
tk.Label(root, text="Count", relief="raised", font=("Curier", 20), width=10).pack()
tk.Button(root, text="AddCnt", command=countUp, width=15, font=("Curier", 16)).pack()

root.mainloop()

例如,我会这样做:

import tkinter as tk

class Example(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("Production")
        self.count_str = tk.IntVar()
        self.count_str.set(0)
        tk.Label(self, textvariable=self.count_str, relief="raised", font=("Curier", 20), width=10).pack()
        tk.Label(self, text="Count", relief="raised", font=("Curier", 20), width=10).pack()
        tk.Button(self, text="AddCnt", command=self.count_up, width=15, font=("Curier", 16)).pack()

    def count_up(self):
        self.count_str.set(self.count_str.get() + 1)

Example().mainloop()

相关问题 更多 >

    热门问题