在我的程序中,为变量分配了tkinter变量,然后对其进行跟踪:
self.tklon = tk.DoubleVar()
self.tklon.trace('w', self.trace_callback)
在GUI中,我有一个Entry-widget来保存此变量:
self.lonEntry = tk.Entry(frame, textvariable = self.tklon)
通常,一切正常。但是,当我删除Entry的内容以输入新值时,Entry在短暂的时间内为空。它仍然会触发跟踪,结果,我收到以下错误消息:
File "xxxx", line 529, in get
return self._tk.getdouble(self._tk.globalgetvar(self._name))
_tkinter.TclError: expected floating-point number but got ""
,在trace_callback()函数中引发错误消息的行是:
self.lon = self.tklon.get()
在我看来,这没有任何进一步的危害,但是终端中连续出现的错误消息令人讨厌。
我当时想可能有解决办法
a)在条目为空时为其设置默认值
b)仅在您按键盘上的Enter键时更新分配给该条目的文本变量
但是我看不到Entry-widget的任何此类选项。我想不出任何有效性检查,因为没有什么比.get()更原始的了,它会引发错误...
我只是错过了正确的信息吗?还有其他解决方法吗?也许甚至没有解决办法?
感谢您能给我任何帮助!
答案 0 :(得分:1)
问题似乎是user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#REMOVE REFERENCE TO FILES THAT HAVE "server" and "location" blocks in them so we can do it all in this file
#include /etc/nginx/conf.d/*.conf;
# issue with ip and the nginx proxy
real_ip_header X-Forwarded-For;
set_real_ip_from 0.0.0.0/0;
server {
listen 80;
listen [::]:80;
server_name example.tech;
location /.well-known/openid-configuration {
proxy_pass https://myapp.net;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
#proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-Host $host;
#proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
}
location /connect {
proxy_pass https://myapp.net;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
#proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-Host $host;
#proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
}
location /auth {
proxy_pass https://myapp.net;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
#proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-Host $host;
#proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
}
}
server {
listen 80;
listen [::]:80;
server_name api.example.tech;
location /auth/ {
proxy_pass https://myapp.net;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
#proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-Host $host;
#proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffers 32 4k;
}
}
}
的本质在tkinter中如何工作。
因此,当值在输入字段中更改时,它将将该值应用于所考虑的变量。这是双人间。将不是双精度兼容字符串的值应用于该变量时会遇到问题。
当您在textvariable
中使用get()
时,实际上是double var试图返回double的尝试。但是,空字符串不能转换为双精度,因此会出错。
一种简单的处理方法是使用DoubleVar()
语句处理错误。
尝试这个想法:
try/except
另一种可能是更好的选择,您可以将import tkinter as tk
root = tk.Tk()
tklon = tk.DoubleVar()
def trace_callback(*args):
try:
print(tklon.get())
except:
print("value not a valid double")
lonEntry = tk.Entry(root, textvariable=tklon).pack()
tklon.trace('w', trace_callback)
root.mainloop()
更改为DoubleVar()
并测试字符串是否为双精度。
StringVar()