我想为通过Serial传入的常量数据创建一个简单的GUI。我决定使用tkinter。值public override void LoadView()
{
view = new UIView()
{
Frame = UIScreen.MainScreen.Bounds,
BackgroundColor = UIColor.White,
};
View = view;
}
已更新,应显示在标签中。我为容器和其他页面创建了单独的类。我将容器定义为:
reading
显示标签的页面:
class Gui(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
container = Frame(self)
container.pack(side="top", fill = "both", expand = TRUE)
container.grid_rowconfigure(0, weight = 1)
self.frames={}
for F in (StartPage, PageOne):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
frame.UpdateMe()
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
现在,初始化GUI:
class PageOne(Frame):
def __init__(self, parent, controller):
Frame.__init__(self,parent)
global reading
self.label1text = StringVar()
self.label1 = Label(self, textvariable = label1text)
self.label1.pack()
button1 = Button (self, text = "Show Start Page", command = lambda: controller.show_frame(StartPage))
button1.pack()
self.label1text.set(reading)
def UpdateMe(self):
global reading
self.lable1text.set(reading)
但是,由于root = Gui()
root.mainloop()
正在阻塞,因此之后的任何参数都不会执行;我可以使用mainloop()
和update
解决这个问题。但是,当我仅创建update_idletasks
的实例时,我仍然不知道如何在UpdateMe()
内调用函数PageOne()
。有什么办法可以解决这个问题,或者可以补救我对类和对象编程的理解?
答案 0 :(得分:0)
由于如果不初始化{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 230,
"max_score": 3.9777644,
"hits": [{
"_index": "data1",
"_type": "doc",
"_id": "sdhniusbfjvbdnfb134u23",
"_score": 3.9777644,
"_source": {
"Pdf_URL": null,
"path": "Whatever/Local/Path",
"host": "suyash",
"message": "Message1",
"Details": "Details1",
"@timestamp": "2019-02-26T10:54:43.733Z",
"Date": "02-05-2012",
"Time": "09:34:49",
"Announcement": "Announcement1",
"@version": "1"
}
},
{
"_index": "data1",
"_type": "d1",
"_id": "uV9xKWkBn8v9OsZrV8Ca",
"_score": 3.9609475,
"_source": {
"path": "Whatever/Local/Path",
"host": "suyash",
"message": "Message2",
"@timestamp": "2019-02-26T10:54:43.768Z",
"Announcement": "Announcement2",
"@version": "1"
}
},
{
"_index": "data1",
"_type": "doc",
"_id": "erhsdfhsdhrsth35y4",
"_score": 3.9609475,
"_source": {
"Pdf_URL": null,
"path": "Whatever/Local/Path",
"host": "suyash",
"message": "Message3",
"Details": "Details3",
"@timestamp": "2019-02-26T10:54:43.727Z",
"Date": "02-11-2012",
"Time": "09:52:57",
"Announcement": "Announcement3",
"@version": "1"
}
},
{
"_index": "data1",
"_type": "doc",
"_id": "asdviyqwbigbqrugvne",
"_score": 3.6811633,
"_source": {
"Pdf_URL": null,
"path": "Whatever/Local/Path",
"host": "suyash",
"message": "Message4",
"Details": "Details4",
"@timestamp": "2019-02-26T10:54:43.734Z",
"Date": "02-12-2011",
"Time": "09:26:05",
"Announcement": "Announcement4",
"@version": "1"
}
},
{
"_index": "data1",
"_type": "doc",
"_id": "g3rewrgewrgserg",
"_score": 3.5482104,
"_source": {
"path": "Whatever/Local/Path",
"host": "suyash",
"message": "Message5",
"@timestamp": "2019-02-26T10:54:43.770Z",
"Announcement": "Announcement5",
"@version": "1"
}
},
{
"_index": "data1",
"_type": "doc",
"_id": "iuabdviusbiovjbsod134",
"_score": 3.5482104,
"_source": {
"Pdf_URL": null,
"path": "Whatever/Local/Path",
"host": "suyash",
"message": "Message6",
"Details": "Details6",
"@timestamp": "2019-02-26T10:54:43.726Z",
"Date": "02-01-2013",
"Time": "10:05:18",
"Announcement": "Announcement6",
"@version": "1"
}
},
{
"_index": "data1",
"_type": "d1",
"_id": "pIVxKWkBs1ExbNgWXw1q",
"_score": 3.5482104,
"_source": {
"path": "Whatever/Local/Path",
"host": "suyash",
"message": "Message7",
"@timestamp": "2019-02-26T10:54:43.765Z",
"Announcement": "Announcement7",
"@version": "1"
}
}
]
}
}
就无法创建StringVar
(对于您的情况是Tk()
),因此您需要在{{1}内创建Gui()
变量}和reading
用作它的Gui()
。以下是基于您的代码的示例:
PageOne.label1
请注意,我已经创建了一个函数textvariable
,以使用from tkinter import *
from random import randint
class Gui(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
container = Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.reading = StringVar() # create the StringVar for PageOne
self.frames = {}
for F in (StartPage, PageOne):
frame = F(container, self)
frame.grid(row=0, column=0, sticky="nsew")
self.frames[F] = frame
self.show_frame(StartPage)
def show_frame(self, cont):
self.frames[cont].tkraise()
class StartPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
button1 = Button (self, text="Show Page 1", command=lambda: controller.show_frame(PageOne))
button1.pack(fill="both", expand=True)
class PageOne(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.label1 = Label(self, textvariable=controller.reading) # refer to Gui.reading StringVar
self.label1.pack(fill='x')
button1 = Button (self, text="Show Start Page", command=lambda: controller.show_frame(StartPage))
button1.pack(fill='x')
# use .after() to simulate the update of reading variable periodically
def update_reading():
app.reading.set(randint(0, 10000))
print('reading:', app.reading.get())
app.after(1000, update_reading)
app = Gui()
update_reading() # start the simulation task of updating reading variable
app.mainloop()
函数定期模拟update_reading()
变量的更新。