[英]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变量( StringVar
, IntVar
等)而不是常规变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.