Skip to main content
 首页 » 编程设计

python之"variable"可能未定义或从 star 导入 : tkinter 定义

2025年05月04日14jyk

我正在尝试为图形用户界面制作一个简单的大纲,但我收到了警告 “variable”可能未定义或从 star imports 定义: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() 

请您参考如下方法:

从 tkinter 导入 *

在这一行中,您从 tkinter 导入所有内容。不推荐这样做,所以 linter 会警告你。但如果你真的想这样做,没关系,忽略它就可以了。

为了更好,您应该显式导入您需要的内容。例如:

from tkinter import Tk, Label, Frame, Entry, Button