当使用pythontkinter时,如何停止同一类中显示相同输入文本的两个输入框?

2024-10-03 06:18:34 发布

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

我对pythontkinter有这个问题。我正在尝试制作一个用户界面窗体屏幕,它要求用户在屏幕上显示的输入框中输入值。在同一个类的界面中设置了两个条目。问题是,当我在其中一个框中键入文本时,我键入的文本不仅显示在输入框中,而且还显示在另一个框中。在

下面是有问题的代码。在

class GenericSkeleton: # The template for all the screens in the program

    def __init__(self): 

        self.GenericGui = Tk()
        self.GenericGui.title('Radial Arc Calculator')
        self.GenericGui.geometry('360x540')
        self.GenericGui.resizable(width = FALSE, height = FALSE)
        Label(self.GenericGui,text = 'Radial Arc Calculator',font = ('Ariel',18)).place(x=65,y=35)

    def destroy(self):

        self.GenericGui.destroy()



class InputScreen(GenericSkeleton):

    def __init__(self):  

        GenericSkeleton.__init__(self)

        Button(self.GenericGui,text = 'CALCULATE',height = 1, width = 25, command = calculate, font = ('TkDefaultFont',14)).place(x=37,y=400)
        Button(self.GenericGui,text = 'CLOSE',height = 1, width = 11, command = close, font = ('TkDefaultFont',14)).place(x=37, y=450)
        Button(self.GenericGui,text = 'HELP', height = 1, width = 11, command = DisplayHelp, font = ('TkDefaultFont',14)).place(x=190, y=450)

        Label(self.GenericGui,text = 'Enter Radius (mm):', font = ('TkDefaultFont',14)).place(x=37, y=180)
        Label(self.GenericGui,text = 'Enter point distance (mm):', font = ('TkDefaultFont',14)).place(x=37, y=250)

        Entry(self.GenericGui,textvariable = Radius, width = 10, font = ('TkDefaultFont',14)).place(x=210, y=180)
        Entry(self.GenericGui,textvariable = Distance, width = 5, font = ('TkDefaultFont',14)).place(x=265, y=250)    

run = InputScreen()

输入框在代码的底部,我希望它足够/不是太多来解决问题。在


Tags: textselfinitdefplacebuttonwidthlabel
1条回答
网友
1楼 · 发布于 2024-10-03 06:18:34

问题是它们共享相同的textvariable(使用不同的变量名,但是它们的值相同,这使得它们在tkinter看来是相同的)。我的建议是不要使用textvariable属性。你不需要它。在

但是,如果您删除了textvariable的使用,那么您需要将小部件的创建与小部件的布局分开,这样就可以保留对小部件的引用。{cd4>而不是使用方法^获得变量:

self.entry1 = Entry(...)
self.entry2 = Entry(...)
self.entry1.place(...)
self.entry2.place(...)

稍后,您可以得到如下值:

^{pr2}$

如果您确实需要textvariable(通常仅当您使用tkinter变量的trace功能时),则必须使用tkinter变量(StringVarIntVar等),而不是常规变量。在

相关问题 更多 >