簡體   English   中英

當從全局實例請求中請求類方法時,Python 消息框不更新。 我在這里缺少什么?

[英]Python Message box not updating when class method is requested from a global instance request. What am I missing here?

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,所以我不知道出了什么問題。 您的問題是“但它不起作用”,但並沒有准確描述它是如何失敗的。 提供的示例似乎工作正常並且沒有重現問題?

避免使用線程 - 你不需要線程從網絡接收某些東西,你可以使用createfilehandler的低級createfilehandler來創建一個事件,當某些東西可以在套接字中讀取時將被調用,而不會凍結 gui 並且沒有線程。 或者如果您更喜歡更高級別(推薦)您可以使用異步框架通信協議!

編輯:從評論中我終於可以看到你的問題是什么:你有兩個Api_Operations類的實例

其中之一被實例化並存儲為名為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()函數中,您可以使用存儲在全局名稱AppAPI_Tabs實例訪問它:

App.Api.PrintMessage()

您可以刪除Api = Api_Operations(App)行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM