简体   繁体   English

Python tkinter Entry小部件无值

[英]None Value with Python tkinter Entry widget

I am reading from a database and I am using ttk.Entry widgets to display the database data. 我正在从数据库中读取数据,并且正在使用ttk.Entry小部件显示数据库数据。 I want the user to see the values and if they want to, change them. 我希望用户看到这些值,如果需要,请更改它们。

But when a database's value is Null ( None value for Python) the Entry widget displays the word "None" and not a None value. 但是,当数据库的值为Null(Python的值为None )时,Entry小部件将显示单词“ None”而不是None值。 Is there any way to have a None value at the Entry widget or I have to compare using the word "None" hoping that the user will not use it? 有什么方法可以使Entry小部件具有None值,或者我必须使用单词"None"进行比较,希望用户不会使用它?

The code just shows the Entry display of a "None" value: 该代码仅显示“无”值的Entry显示:

from tkinter import *
from tkinter import ttk

root = Tk()

entry_value = StringVar()
entry = ttk.Entry(root, textvariable=entry_value)
entry.pack(padx=10, pady=10)
ttk.Button(root, text='Exit', command=root.destroy).pack(padx=10, pady=10)

myvalue = None
entry_value.set(myvalue)
print('same values') if myvalue == entry_value.get() else print('different values')
print('myvalue is None') if myvalue is None else print('myvalue is not None')
print('entry_value is None') if entry_value.get() is None else print('entry_value is not None')
print('entry_value is the word None') if entry_value.get() == 'None' else print ('entry_value is not thr word None')

root.mainloop()

Tkinter widgets cannot represent the value None . Tkinter小部件不能表示值None Tkinter is just a thin python wrapper around a tcl interepter, and the tcl language has no notion of None or a null value. Tkinter只是围绕tcl拦截器的python薄包装,tcl语言没有None或null值的概念。 Since str(None) returns the string "None" , that is what the widget is set to when you give it the value of None . 由于str(None)返回字符串"None" ,因此当您将其赋予None值时,这就是该窗口小部件所设置的。

You'll have to come up with some custom way to handle that yourself, such as inserting an empty string if the value is actually None . 您必须想出一些自定义方式来自己处理,例如,如果值实际上是None则插入一个空字符串。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM