我是Python的新手。
单击“检查连接”按钮,我希望消息显示在tkinter窗口本身,而不是消息框...我希望每个按钮功能的输出都在tkinter中的相同位置。< / p>
from Tkinter import *
import subprocess
root = Tk()
root.geometry("850x500+300+300")
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Windows")
#self.style = Style()
#self.style.theme_use("default")
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
lbl = Label(self, text="Windows")
lbl.grid(sticky=W, pady=4, padx=5)
abtn = Button(self, text="Check Wired Connections",command = self.checkconnections)
abtn.grid(row=1, column=3)
cbtn = Button(self, text="Check Wireless Connections")
cbtn.grid(row=3, column=3, pady=4)
obtn = Button(self, text="OK")
obtn.grid(row=5, column=3)
self.v = StringVar()
messagelabel=Label(self, textvariable=self.v)
messagelabel.grid(row=1, column=0, pady=0)
def checkconnections(self):
p = subprocess.call('nm-tool')
print p
self.v.set(p)
def main():
app = Example(root)
root.mainloop()
main()
答案 0 :(得分:2)
然后在班级中创建标签
class Example(Frame):
.....
self.v = StringVar()
messagelabel=Label(self, textvariable=v).pack()
self.v.set("")
......
def checkconnections():
p1 = subprocess.check_output('nm-tool' )
self.v.set('nm-tool', p1)
from Tkinter import *
import subprocess
root = Tk()
root.geometry("850x500+300+300")
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Windows")
#self.style = Style()
#self.style.theme_use("default")
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
lbl = Label(self, text="Windows")
lbl.grid(sticky=W, pady=4, padx=5)
abtn = Button(self, text="Check Wired Connections",command = self.checkconnections)
abtn.grid(row=1, column=3)
cbtn = Button(self, text="Check Wireless Connections")
cbtn.grid(row=3, column=3, pady=4)
obtn = Button(self, text="OK")
obtn.grid(row=5, column=3)
self.v = StringVar()
messagelabel=Label(self, textvariable=self.v)
messagelabel.grid(row=1, column=0, pady=0)
def checkconnections(self):
self.v.set("hai")
def main():
app = Example(root)
root.mainloop()
main()