我正在尝试为 gui 制作一个简单的大纲,但我收到警告“变量”可能未定义或从 star 导入定义:我所有变量的 tkinter。
这是我的代码:
from tkinter import *
class myApp :
def __init__(self, gui,) :
self.root = gui
self.bframe = Frame(self.root) # Create a container Frame at bottom
self.bframe.pack(side=BOTTOM)
self.xlabel = Label(self.root, text="Item ID") # Create the Label
self.xlabel.pack(side=LEFT)
self.xentry = Entry(self.root, bd=5) # Create the Entry box
self.xentry.pack(side=LEFT)
self.xentry.bind('<Return>', self.showStockItem)
self.xentry.focus_set() # Set focus in the Entry box
self.xopen = Button(self.root, text="Show", command=self.showStockItem) # Create the open Button
self.xopen.pack(side=LEFT)
self.xquit = Button(self.bframe, text="Quit", command=self.quitit) # Create the quit Button
self.xquit.pack(side=BOTTOM)
return
gui = Tk()
gui.title("Travel")
app = myApp(gui)
gui.mainloop()
Run Code Online (Sandbox Code Playgroud)
from tkinter import *
在这一行中,您从tkinter. 不建议这样做,因此 linter 会警告您。但是如果你真的想这样做,没关系,直接忽略它。
为了更好,您应该明确导入您需要的内容。例如:
from tkinter import Tk, Label, Frame, Entry, Button
Run Code Online (Sandbox Code Playgroud)
考虑使用:
import tkinter as tk
Run Code Online (Sandbox Code Playgroud)
然后,为所有调用添加前缀,例如:
root = tk.Tk()
Run Code Online (Sandbox Code Playgroud)
或者,
variableName.pack(side = tk.LEFT)
Run Code Online (Sandbox Code Playgroud)
等等...