简体   繁体   English

Tkinter window 出现得太早

[英]Tkinter window showing up too early

I'm trying to build a binary lamp in Python using Tkinter.我正在尝试使用 Tkinter 在 Python 中构建二元灯。 The steps are as follows:步骤如下:

    1. Prompt user for input, using the console/shell.
    2. Draw the lamp
    3. Use mainloop to show the image

However, this doesn't always work as I expect it to.但是,这并不总是像我期望的那样工作。

The code:编码:

    from tkinter import *

    HEIGHT = 235
    WIDTH = 385

    tk = Tk()
    canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="grey")


    size = 35

    canvas.pack()
    def dec_to_binh(num, lst):
        if num>1:
            dec_to_binh(num // 2, lst)
        lst.append(num%2)
        return lst
    def dec_to_bin(num):
        answ = dec_to_binh(num, [])
        while len(answ) < 4:
            answ.insert(0,0)
        return answ
    class Diode:
        def __init__(self, x, y, label):
            self.shape = canvas.create_oval(x, y, x+size, y+size, width=4,
                                    outline='black', fill='#232323')
            self.label = canvas.create_text(x+int(size/2), y+size+10, text=str(label))
            self.onOff = 0
        def update(self, num):
            if int(num) == 1:
                canvas.itemconfig(self.shape, fill=oncolor)
                onOff = 1
            else:
                canvas.itemconfig(self.shape, fill='black')
                onOff = 0

    class Lamp:
        def __init__(self):
            self.LEDs = [Diode(100, 100, 8), Diode(150, 100, 4), Diode(200, 100, 2), Diode(250, 100, 1)]
    
        def update(self, number):
            n=0
            for i in self.LEDs:
                i.update(dec_to_bin(number)[n])
                n=n+1


    print("What to show as a binary lamp?")

    answer=int(input())
    while answer > 15 or answer < 0:
        print("Invalid input, can only show between range of 0 to 15 inclusive")
        answer=int(input())
    lamp = Lamp()
    lamp.update(answer)

    tk.mainloop()

The bug: When I run this in my IDE (Wing 101), it patiently waits for me to input the number I want to show before drawing the image.错误:当我在 IDE(Wing 101)中运行此程序时,它会耐心等待我输入要显示的数字,然后再绘制图像。 However, if I run it straight from the python shell (ie I just click on bin_lamp.py) it opens and creates the tk window and shows a blank screen, without waiting for me to tell it what number I want.但是,如果我直接从 python shell 运行它(即我只需单击 bin_lamp.py),它就会打开并创建 tk window 并显示一个空白屏幕。

After I actually enter the number, it draws what I want it to, but is there any way I can stop the blank tk window with the blank canvas from actually showing up until I'm done entering the input?在我实际输入数字之后,它会画出我想要的数字,但是有什么办法可以阻止空白 tk window 与空白 canvas 实际显示,直到我完成输入输入?

You can either put the input before you create the window with tk = Tk() , or you can leave everything as is, but hide the window and show it once it's done.您可以在使用tk = Tk()创建 window 之前输入输入,或者您可以保留所有内容,但隐藏 window 并在完成后显示它。 Simply hide the window after it has been created and show it afterwards:只需在创建 window 后隐藏它并在之后显示它:

tk = Tk()
tk.withdraw()
# do all your processing
tk.deiconify()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM