python 27 输入字符串使用 tkSimpleDialog
python 27 input string using tkSimpleDialog
我在 Windows 7 和 python27 上使用带有 Tkinter 的简单 gui 来控制我的程序。此外,我想避免使用许多额外的包,因为我稍后会冻结程序,并且我想避免 "exotic" 包出现问题。
在一种情况下,我必须更改传感器名称,我使用的是这样一个简单的函数:
def change_sensorname():
new_sensorname = tkSimpleDialog.askstring("Sensorname", "Please input the new sensorname")
它工作正常,但在这种情况下如何限制接受的字符?我想在输入 window 关闭之前捕获错误的字符串。如果这不可能,我就打开另一个。
我想限制最大长度,我也想限制不同的字符。例如只有 5 个字符,并且只有 A-Z 和“_”中的字母。
有没有简单的方法来过滤这个?比如你使用askinteger,你可以设置min和maxvalue,但是对于askstring没有一定的限制。
干杯最大
看起来像 askstring does not have such functionality,所以做一个循环:
def meets_sensorname_criteria(sensorname):
max_len = 10
restricted_chars = ('@', '!', '?')
return (len(sensorname) < max_len
and not any((char in sensorname) for char in restricted_chars))
def change_sensor_name():
new_sensorname = tkSimpleDialog.askstring("Sensorname", "Please input the new sensorname")
while not meets_sensorname_criteria(new_sensorname):
# Some warning alert here to explain expected input might be good
new_sensorname = tkSimpleDialog.askstring("Sensorname", "Please input the new sensorname")
编辑:
替代方案确实看起来像是在滚动您自己的对话框 class。但是,如果 source for tkSimpleDialog
是任何指示,那么通过继承 Dialog
和 replicating/modifying _QueryDialog
,获得一组类似的功能将需要相当多的代码行,_QueryString
和 askstring
.
你可以尝试直接从_QueryString
继承,但不能说我推荐它。
我在 Windows 7 和 python27 上使用带有 Tkinter 的简单 gui 来控制我的程序。此外,我想避免使用许多额外的包,因为我稍后会冻结程序,并且我想避免 "exotic" 包出现问题。
在一种情况下,我必须更改传感器名称,我使用的是这样一个简单的函数:
def change_sensorname():
new_sensorname = tkSimpleDialog.askstring("Sensorname", "Please input the new sensorname")
它工作正常,但在这种情况下如何限制接受的字符?我想在输入 window 关闭之前捕获错误的字符串。如果这不可能,我就打开另一个。
我想限制最大长度,我也想限制不同的字符。例如只有 5 个字符,并且只有 A-Z 和“_”中的字母。
有没有简单的方法来过滤这个?比如你使用askinteger,你可以设置min和maxvalue,但是对于askstring没有一定的限制。
干杯最大
看起来像 askstring does not have such functionality,所以做一个循环:
def meets_sensorname_criteria(sensorname):
max_len = 10
restricted_chars = ('@', '!', '?')
return (len(sensorname) < max_len
and not any((char in sensorname) for char in restricted_chars))
def change_sensor_name():
new_sensorname = tkSimpleDialog.askstring("Sensorname", "Please input the new sensorname")
while not meets_sensorname_criteria(new_sensorname):
# Some warning alert here to explain expected input might be good
new_sensorname = tkSimpleDialog.askstring("Sensorname", "Please input the new sensorname")
编辑:
替代方案确实看起来像是在滚动您自己的对话框 class。但是,如果 source for tkSimpleDialog
是任何指示,那么通过继承 Dialog
和 replicating/modifying _QueryDialog
,获得一组类似的功能将需要相当多的代码行,_QueryString
和 askstring
.
你可以尝试直接从_QueryString
继承,但不能说我推荐它。