简体   繁体   English

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

[英]When using python Tkinter, how can I stop two entry box's in the same class showing the same input text?

I am having this issue with Python Tkinter. 我在Python Tkinter中遇到这个问题。 I am trying to make a user interface form screen which requires the user to enter values into entry box's displayed on screen. 我试图制作一个用户界面表单屏幕,要求用户在屏幕上显示的输入框内输入值。 I have set it so the two Entry Box's are in the same class (that class being the interface screen). 我已将其设置为使两个输入框位于同一类中(该类为界面屏幕)。 The problem is that while I type into one of the box's, the text which I type not only displays in the box in which I am typing into, but also in the other box. 问题是,当我键入一个框时,键入的文本不仅显示在我要键入的框中,而且还显示在另一个框中。

Below is the code in question. 下面是有问题的代码。

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()

The entry box's are at the bottom of the code, I hope its enough/not too much to solve the problem. 输入框位于代码的底部,我希望它足够/不要太多以解决问题。

The problem is that they both share the same textvariable (you use different variable names, but they have the same value which makes them the same in the eyes of tkinter). 问题在于它们都共享相同的textvariable变量(您使用了不同的变量名,但是它们具有相同的值,这使得它们在textvariable是相同的)。 My advice is to not use the textvariable attribute. 我的建议是不要使用textvariable属性。 You don't need it. 不用了

However, if you remove the use of textvariable then you need to separate your widget creation from widget layout so that you can keep a reference to the widget. 但是,如果删除对textvariable的使用,则需要将窗口小部件创建与窗口小部件布局分开,以便可以保留对窗口小部件的引用。 Then you can use the get method on the widget (rather than on the variable) to get the value: 然后,您可以在小部件(而不是变量)上使用get方法来获取值:

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

Later, you can get the values like this: 稍后,您可以获取如下所示的值:

radius = int(self.entry1.get())
distance = int(self.entry2.get())

If you do need the textvariable (usually only if you're using the trace feature of a tkinter variable), you must use a tkinter variable ( StringVar , IntVar , etc) rather than a regular variable. 如果确实需要textvariable (通常仅在使用textvariable变量的trace功能时),则必须使用tkinter变量( StringVarIntVar等)而不是常规变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得Tkinter条目小部件,使其与普通的python输入相同 - How can I get a Tkinter entry widget to work the same as a normal python input Python Tkinter-具有相同验证的多个输入框 - Python Tkinter - Multiple entry box with same validation 如何在 tkinter 中同时使用 playsound 和输入框播放音频? - How can I play audio with playsound and type in an entry box at the same time in tkinter? 如何将输入输入框 (tkinter) 的文本分配给 python 脚本中的变量并通过按下按钮运行脚本? - How can I assign the text imputed into an entry box (tkinter) to variable in python script and run the script by pressing a button? 我如何从输入小部件,tkinter,python获取输入 - how can i get the input from the entry widget, tkinter, python 文字和输入宽度不一样 - Text and entry width not the same tkinter 如何将条目输入保存到 tkinter 中的变量我正在使用 python 3 - How to save entry input to variable in tkinter i'm using python 3 当我在课堂上按下 Python Tkinter 中的按钮时如何删除条目 - How to delete an entry when I press a Button in Python Tkinter in a class 在tkinter的输入框中显示文本的问题 - issue with showing text in entry box in tkinter 如何将tkinter按钮与标签和输入框保持在同一行 - How to keep tkinter button on same row as label and entry box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM