而在Python中循环以在窗口中更新
我已经创建了一个脚本,它的目的是在insert字段中插入一个变量。 到目前为止,我已经能够创建打印变量的而在Python中循环以在窗口中更新,python,tkinter,Python,Tkinter,我已经创建了一个脚本,它的目的是在insert字段中插入一个变量。 到目前为止,我已经能够创建打印变量的while循环,因此我知道它的工作原理 令人沮丧的是,它没有打开窗口,只打印变量: import time import subprocess from Tkinter import * import urllib2 from ttk import * import Tkinter as tk root = Tk() a= 0 while a<1: print a
while
循环,因此我知道它的工作原理
令人沮丧的是,它没有打开窗口,只打印变量:
import time
import subprocess
from Tkinter import *
import urllib2
from ttk import *
import Tkinter as tk
root = Tk()
a= 0
while a<1:
print a
class Application(Frame):
"""GUI to display results of 'equity get'"""
def __init__(self, master):
"""initialise the Frame"""
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""Create button, text and entry Widget"""
"""what it is i.e. label"""
self.data = Label(self, text ="Data")
self.data1 = Entry(self, width = 10)
self.data.grid(column = 1, row = 1)
self.data1.grid(column = 2, row = 1)
self.data1.grid(column = 3, row = 1)
self.data1.insert(0,a)
app = Application(root)
root.title("reload test")
root.geometry("700x300")
root.mainloop()
代码中最大的问题可能是在另一个循环中调用
mainloop
。这毫无意义。Tkinter设计用于只调用一次mainloop
,通常作为程序中的最后一个可执行行或主函数中的最后一个可执行行
在循环中定义类也是没有意义的。恐怕这段代码需要完全重构
最后,您从未看到窗口内容的原因是您没有请求使其可见。您正在创建
应用程序
的实例,它是框架
的子类。但是,您忽略了在该实例上调用pack
,place
或grid
,因此它保持不可见 当主循环退出时,它将再次启动应用程序。您几乎肯定不想定义一个类并在无限循环中运行GUI mainloop。你到底想在这里实现什么?这是root.mainloop()的原因,它本身就是一个循环。如果您想在此循环中进行一些更新,可以使用after
和处理更新的函数,请参阅本文:
import time
import subprocess
from Tkinter import *
import urllib2
from ttk import *
import Tkinter as tk
root = Tk()
a= 0
while a<1:
print a
class Application(Frame):
"""GUI to display results of 'equity get'"""
def __init__(self, master):
"""initialise the Frame"""
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""Create button, text and entry Widget"""
"""what it is i.e. label"""
self.data = Label(self, text ="Data")
self.data1 = Entry(self, width = 10)
self.data.grid(column = 1, row = 1)
self.data1.grid(column = 2, row = 1)
self.data1.grid(column = 3, row = 1)
self.data1.insert(0,a)
app = Application(root)
root.title("reload test")
root.geometry("700x200")
root.mainloop()
import time
import subprocess
from Tkinter import *
import urllib2
from ttk import *
import Tkinter as tk
root = Tk()
a= 0
while a<1:
url = "https://uk.finance.yahoo.com/q?s=ngq14.nym&ql=1"
request= urllib2.Request(url)
handle = urllib2.urlopen(request)
content = handle.read()
splitted_page24 = content.split("<span id=\"yfs_l10_ngq14.nym\">", 1);
splitted_page24 = splitted_page24[1].split("</span>", 1)
print splitted_page24[0]
class Application(Frame):
"""GUI to display results of 'equity get'"""
def __init__(self, master):
"""initialise the Frame"""
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""Create button, text and entry Widget"""
"""what it is i.e. label"""
self.data = Label(self, text ="Data")
self.data1 = Entry(self, width = 10)
self.data.grid(column = 1, row = 1)
self.data1.grid(column = 2, row = 1)
self.data1.grid(column = 3, row = 1)
self.data1.insert(0,splitted_page24[0])
app = Application(root)
root.title("reload test")
root.geometry("700x300")
root.mainloop()
root.after(15)