这个tkinter程序怎么了? - python

Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        

import random
import time
from tkinter import *    

root = Tk()

x = ""

lab = Label(root,text = x)
lab.pack()

root.mainloop()

def randomno():
    while (1):
        y = random.randint(1, 100)
        y = StringVar()
        x = y.get()
        lab["text"] = x
        #root.update_idletasks()
        time.sleep(2)

randomno()

错误:

Traceback (most recent call last):   File
   "C:/Users/Acer/PycharmProjects/unseen/tp.py", line 26, in <module>
       randomno()   File "C:/Users/Acer/PycharmProjects/unseen/tp.py", line 20, in randomno
       y = StringVar()   File "C:\Users\Acer\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py",
   line 480, in __init__
       Variable.__init__(self, master, value, name)   File "C:\Users\Acer\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py",
   line 317, in __init__
       self._root = master._root() AttributeError: 'NoneType' object has no attribute '_root'

python参考方案

这是在tkinter中执行所需操作的常用方法:

import random
import time
import tkinter as tk

DELAY = 2000  # milliseconds (thousandth of a second)

def randomno():
    x = random.randint(1, 100)
    lab["text"] = x
    #time.sleep(2)  # Don't call this in a tkinter program!
    root.after(DELAY, randomno)  # Call this function again after DELAY ms.


root = tk.Tk()

lab = tk.Label(root, text="")
lab.pack()

randomno()  # Starts periodic calling of itself.
root.mainloop()

您无需使用StringVar,只需在randomno()函数中分配新的随机值即可。

您不应该在time.sleep()应用程序中调用tkinter。请改用通用窗口小部件方法after()。注意上面的代码中randomno()如何调用root.after()以便以后再次调用它的方式。

这就是定期在tkinter程序中执行某些操作的方法,这种方法将在调用sleep()时保持GUI不会“挂起”运行。

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

查找字符串中的行数 - python

我正在创建一个python电影播放器​​/制作器,我想在多行字符串中找到行数。我想知道是否有任何内置函数或可以编写代码的函数来做到这一点:x = """ line1 line2 """ getLines(x) python大神给出的解决方案 如果换行符是'\n',则nlines …

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

字符串文字中的正斜杠表现异常 - python

为什么S1和S2在撇号位置方面表现不同?S1="1/282/03/10" S2="4/107/03/10" R1="".join({"N\'" ,S1,"\'" }) R2="".join({"N\'…