简体   繁体   English

如何在不停止脚本的情况下随机弹出一些gui #python 2.7

[英]How to make randomly pop some gui without stopping the script #python 2.7

I'm relatively new to python, so please bear with me. 我对python比较陌生,所以请多多包涵。

My question has two aspects: 我的问题有两个方面:

  • First, I'm trying to make a GUI that randomly pops different sentences to the user, every time in a new frame. 首先,我试图制作一个GUI,每次在新框架中向用户随机弹出不同的句子。

  • Second, I want the user to be able to close the GUI without stopping the script, like if it was running in the background. 其次,我希望用户能够在不停止脚本的情况下关闭GUI,就像脚本在后台运行一样。

here is my code: 这是我的代码:

import Tkinter as tk
import random
number = random.randint(0,13)

sentence = {contains 13 sentences
    }

window = tk.Tk()

window.output = tk.Label(text = sentence[number])
window.title("I Always See You")
window.geometry("300x150")

def next():
    rand= random.randint(0, 13)
    window2 = tk.Tk()
    window2.output = tk.Label(text = sentence[rand])
    window2.output.pack(side="top", fill="x", expand=True)
    window2.title("I Always See You")
    window2.geometry("300x150")

window.output.pack(side="top", fill="x", expand=True)

choice()

window.after(1000, next)
window.mainloop()

My problem: when my second frame pops, there isn't any text showing, and if it does have something popping, it appears in the first frame. 我的问题是:当第二个框架弹出时,没有任何文本显示,并且如果确实有弹出的东西出现在第一个框架中。

also, how can you insert a random float in .after() ? 另外,如何在.after()中插入随机浮点数?

Thank you so much for your help! 非常感谢你的帮助!

cheers 干杯

You do not see the text in the second window because Tkinter cannot handle two main windows. 您不会在第二个窗口中看到该文本,因为Tkinter无法处理两个主窗口。 You need to use the Toplevel class for the others. 您需要将Toplevel类用于其他类。 In addition, you haven't specified the parent of the label in next , so it will probably be packed inside window instead of window2 . 另外,您还没有在next指定标签的父级,因此它很可能打包在window而不是window2

In addition, you need 14 sentences because randint , unlike randrange includes both end points. 另外,您需要14个句子,因为randint不同于randrange包含两个端点。

To set a random time in after , just use randint because it expects an integer number of ms. 要在after设置一个随机时间,只需使用randint因为它需要整数毫秒。

To achieve what you want I suggest you to create a main window that will be withdraw (it will run in the background). 为了实现您想要的功能,建议您创建一个将退出的主窗口(它将在后台运行)。 Then popup Toplevels with random sentences. 然后弹出带有随机句子的“顶级”。 You need to call again after inside the next function if you want the windows to keep poping up. 您需要再次拨打after里面next ,如果你想窗户,保持坡平了功能。 To prevent the after to be cancelled if the user closes the Toplevel, I called the after method from the withdrawn main window: 为了防止用户关闭顶层after取消,我从撤回的主窗口中调用after方法:

import Tkinter as tk
import random
number = random.randint(0,13)

sentence = {i: str(i) for i in range(14)}

def next():
    rand=random.randint(0, 13)
    window2 = tk.Toplevel(root)
    window2.output = tk.Label(window2, text=sentence[rand])
    window2.output.pack(side="top", fill="x", expand=True)
    window2.title("I Always See You")
    window2.geometry("300x150")
    tps = random.randint(1000, 10000)
    root.after(tps, next)

root = tk.Tk()
root.withdraw() # hide root window

window = tk.Toplevel(root)

window.output = tk.Label(window, text=sentence[number])
window.title("I Always See You")
window.geometry("300x150")
window.output.pack(side="top", fill="x", expand=True)

tps = random.randint(1000, 10000)
root.after(tps, next)

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python中的通知而不停止脚本 - Notification in python without stopping the script Python IDLE:如何运行整个脚本,因为它总是在某个时刻停止 - Python IDLE : how to run the whole script as it is always stopping at some point 在不停止部分Progam的情况下捕获Python 2.7中的警告 - Catch Warning in Python 2.7 Without Stopping Part of Progam Python 'for' 循环随机停止 - Python 'for' loop stopping randomly Python 2.7:如何在运行python gui .exe脚本时摆脱cmd窗口 - Python 2.7 : How to get rid of cmd window while running a python gui .exe script 如何在不停止程序的其余部分的情况下延迟python - How to make delay in python without stopping the rest of the program 用于抓取 web gui 字符串的脚本,Python 2.7 上的错误在 Python 3 上运行 - Script to scrape web gui for string, errors on Python 2.7 runs on Python 3 在使用Python 3.x的脚本中,使用Python 2.7导入其他脚本时,如何在导入时使该特定脚本与2.7分开运行? - In a script using Python 3.x, importing other script using Python 2.7, how to make that specific script running with 2.7 separately when importing it? 我想为某些功能制作gui,但它不像我想要的那样,我正在使用python 2.7和Tkinter? - i wanna make gui for some function but it's not like what i want i am using python 2.7 and it's Tkinter? Python GUI-2.7至3.5 - Python GUI - 2.7 to 3.5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM