python - 无法在 Tkinter 中更改标签文本
问题描述
您好我已经为我的 Python Pinger 设置了一个 GUI,但不知何故我无法更改放置在程序主类中的标签。
这是主要脚本:
import sys
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
import PyngIT_support
def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = tk.Tk()
top = PythonPingIT (root)
PyngIT_support.init(root, top)
root.mainloop()
w = None
def create_PythonPingIT(rt, *args, **kwargs):
'''Starting point when module is imported by another module.
Correct form of call: 'create_PythonPingIT(root, *args, **kwargs)' .'''
global w, w_win, root
#rt = root
root = rt
w = tk.Toplevel (root)
top = PythonPingIT (w)
PyngIT_support.init(w, top, *args, **kwargs)
return (w, top)
def destroy_PythonPingIT():
global w
w.destroy()
w = None
class PythonPingIT:
def __init__(self, top=None):
'''This class configures and populates the toplevel window.
top is the toplevel containing window.'''
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black'
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85'
_ana2color = '#ececec' # Closest X11 color: 'gray92'
self.style = ttk.Style()
if sys.platform == "win32":
self.style.theme_use('winnative')
self.style.configure('.',background=_bgcolor)
self.style.configure('.',foreground=_fgcolor)
self.style.configure('.',font="TkDefaultFont")
self.style.map('.',background=
[('selected', _compcolor), ('active',_ana2color)])
top.geometry("1200x800+373+87")
top.minsize(176, 1)
top.maxsize(1924, 1050)
top.resizable(1, 1)
top.title("PyngIT")
top.configure(background="#e4e4e4")
top.configure(highlightbackground="#d9d9d9")
top.configure(highlightcolor="black")
self.Frame_StaniceA = tk.LabelFrame(top)
self.Frame_StaniceA.place(relx=0.006, rely=0.0, relheight=0.331
, relwidth=0.985)
self.Frame_StaniceA.configure(relief='groove')
self.Frame_StaniceA.configure(foreground="black")
self.Frame_StaniceA.configure(text='''Stanice A''')
self.Frame_StaniceA.configure(background="#d9d9d9")
self.Frame_StaniceA.configure(highlightbackground="#d9d9d9")
self.Frame_StaniceA.configure(highlightcolor="black")
self.Motol = tk.Label(self.Frame_StaniceA)
self.Motol.place(relx=0.007, rely=0.083, height=31, width=95
, bordermode='ignore')
self.Motol.configure(activebackground="#f9f9f9")
self.Motol.configure(activeforeground="black")
self.Motol.configure(background="#d9d9d9")
self.Motol.configure(disabledforeground="#a3a3a3")
self.Motol.configure(foreground="#000000")
self.Motol.configure(highlightbackground="#d9d9d9")
self.Motol.configure(highlightcolor="black")
self.Motol.configure(text='''N. Motol''')
if __name__ == '__main__':
vp_start_gui()
这是支持脚本:
import sys
import multiping
import os
from threading import Thread
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
def init(top, gui, *args, **kwargs):
global w, top_level, root
w = gui
top_level = top
root = top
def destroy_window():
# Function which closes the window.
global top_level
top_level.destroy()
top_level = None
def ping_a_comm():
t = Thread(target=ping_A)
t.start()
pass
def ping_A():
import PyngIT as p
os.system("ping 192.168.0.1")
p.PythonPingIT.Motol.configure(text="TEXT")
pass
if __name__ == '__main__':
import PyngIT
PyngIT.vp_start_gui()
当我尝试更改标签时,我得到“ type object 'PythonPingIT' has no attribute 'Motol'
”。所以我想也许我需要像这样调用这个类:
p.PythonPingIT().Motol.configure(text="text")
但只有这个
Exception has occurred: AttributeError
'NoneType' object has no attribute 'geometry'
File "C:\Users\admin\Desktop\PythonPingIt\PyngIT.py", line 67, in __init__
top.geometry("1200x800+373+87")
File "C:\Users\admin\Desktop\PythonPingIt\PyngIT_support.py", line 45, in ping_A
p.PythonPingIT().Motol.configure(text="hey")
我真的迷路了,如果有人知道如何解决这个问题,我将不胜感激
编辑:// 我设法使用以下代码访问该值:
def ping_A():
global w, top_level, root
import PyngIT as p
root = tk.Tk()
w = p.PythonPingIT (root)
os.system("ping localhost")
w.Motol.configure(text="FUUUu")
pass
但我仍然无法更改文本,即使使用w.Motol.pack()
orw.update()
解决方案
推荐阅读
- c# - 获得无效的差异:类型参数必须是通用接口的不变有效错误
- javascript - JavaScript 中的任一类型实现
- python - 如何为 lineEdit 更改 QValidator 的行为:
- python-3.x - Selenium:单击下拉菜单中的菜单不起作用
- swift - 如何裁剪由 AVCaptureMovieFileOutput 录制的视频?
- google-analytics - 谷歌分析:创建一个排除访问特定 URL 的所有用户的视图?
- android - 使用 LiveData、协程和 MockK 测试多个视图状态的 ViewModel 单元
- python - 从 Postman 表单数据中请求键的值
- sql - 如何连续获得所有员工的拳头?
- google-bigquery - 如何在 bigquery 中插入版权符号