import tkinter as TK
from tkinter import *
from tkinter import ttk
############################################
root = TK.Tk()
root.geometry("1000x700+1+5")
WindowTitle = 'Test MessageBox'
Revision = 'Support 1.0';
gServerData = 'Let there be Rock'
# COMMAND/Communication Message Window Test Button #############
def ButtonTest():
global gServerData
#gServerData = '128,TestingMojo'
print('* ButtonTest Function: Ready to Print in MessageBox',gServerData)
Api.PrintMessage()
# TABS Notebook page Setup
class API_Tabs(TK.Frame):
def __init__(self,*args,**kwargs):
Frame.__init__(self,*args,**kwargs)
self.notebook = ttk.Notebook()
self.Tabs()
self.notebook.place(x=5, y = 1, height=690, width =990)
self.master.title(WindowTitle)
#self.MessageHistoryText = MessageHistoryText
#Setup ttk Notebook style
def Tabs(self):
page1 = Api_Select(self.notebook)
page2 = Api_Operations(self.notebook)
self.notebook.add(page1,text="Main")
self.notebook.add(page2,text="Operation")
############################################
class Api_Select(Frame):
def __init__(self,name,*args,**kwargs):
Frame.__init__(self,*args,**kwargs)
self.name = name
class Api_Operations(Frame):
def __init__(self,name,*args,**kwargs):
Frame.__init__(self,*args,**kwargs)
self.name = name
self.createWidgets()
self.PrintMessage()
def PrintMessage(self):
global gServerData
print('* Print Message Function: Message Ready',gServerData)
self.MessageHistoryText.delete('1.0',END)
self.MessageHistoryText.insert(TK.END, 'GlobalServer:' + gServerData + '\n', 'notice')
self.MessageHistoryText.see(TK.END)
self.MessageHistoryText.update()
root.update()
print('* Print Message Function: MessageBox Update',gServerData)
def createWidgets(self):
self.mainFrame = Frame(self) #TK.Frame(self)
self.mainFrame.place(x=0, y = 0, height=650, width = 1000)
self.ButtonTestPB = TK.Button(self.mainFrame, text='TEST', command= ButtonTest)
self.ButtonTestPB.place(x=50, y = 100, height=50, width = 50)
# COMMAND/Communication Entry Window and Send Button #############
self.MessageFrame = TK.Frame(self.mainFrame)
self.MessageFrame.place(x=240, y = 50, height=370, width = 360)
self.MessageCmdLabel = TK.Label(self.MessageFrame, text= 'Command/Communications',anchor="w" )
self.MessageCmdLabel.place(x=0, y = 0, height=25, width = 200)
self.MessageHistoryText = TK.Text(self.MessageFrame, height=20, width=40, relief=TK.SUNKEN)
self.MessageHistoryScroll = TK.Scrollbar(self.MessageFrame)
self.MessageHistoryText.config(yscrollcommand=self.MessageHistoryScroll.set)
self.MessageHistoryScroll.config(command=self.MessageHistoryText.yview)
self.MessageHistoryText.place(x=0, y = 25, height=300, width = 350)
self.MessageHistoryScroll.place(x=350, y = 25, height=300, width = 15)
self.MessageHistoryText.tag_configure('warning', foreground='red', background='gainsboro')
self.MessageHistoryText.tag_configure('notice', foreground='blue', background='gainsboro')
App = API_Tabs(root)
Api = Api_Operations(App)
#_thread.start_new_thread(Receive, ("Receive_Thread",))
App.mainloop()
root.destroy()
''' 使用 ops 页面上的 Button 应该给我与消息框中的 statup 文本相同的结果。这是一个非常大的 GUI 的小例子。我有一个线程正在运行,它在接收到数据时调用一个全局函数。这一切都有效。我需要做的是将接收到的数据打印到我的操作页面消息框而不需要它。测试按钮是模仿请求。
但它不起作用。有什么想法吗?'''
我运行你的代码,点击按钮,看到了print()
s,所以我不知道哪里出了问题。您的问题是“但它不起作用”,但并没有准确描述它是如何失败的。提供的示例似乎工作正常并且没有重现问题?
避免使用线程 - 你不需要线程从网络接收某些东西,你可以使用 tkinter 的低级createfilehandler
来创建一个事件,当某些东西可以在套接字中读取时将被调用,而不会冻结 gui 并且没有线程. 或者如果您更喜欢更高级别(推荐)您可以使用异步框架通信协议!
编辑:从评论中我终于可以看到您的问题是什么:您有两个Api_Operations
class实例。
其中之一被实例化并存储为名为 的全局变量Api
:
Api = Api_Operations(App)
另一个是在API_Tabs
类内部的Tabs()
方法中创建的,并存储为名为 的局部变量page2
:
page2 = Api_Operations(self.notebook)
由于您正在显示page2
但正在更新Api
,更新永远不会显示,因为您正在更新的实例不是屏幕中显示的实例。
要修复它,请将您创建的实例存储API_Tabs.Tabs()
为属性:
def Tabs(self):
page1 = Api_Select(self.notebook)
self.Api = Api_Operations(self.notebook)
self.notebook.add(page1,text="Main")
self.notebook.add(self.Api, text="Operation")
然后在您的ButtonTest()
函数中,您可以使用API_Tabs
存储在全局名称中的实例访问它App
:
App.Api.PrintMessage()
你可以删除你的Api = Api_Operations(App)
线。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句