【问题标题】:Python New TKinter Window Appears when MessageBox is Shown显示 MessageBox 时出现 Python New TKinter 窗口
【发布时间】:2019-04-13 21:08:33
【问题描述】:

显示消息框时,总是会弹出一个新的 Tkinter 窗口。为什么会这样?我没有创建一个新窗口。如何摆脱窗口?

下面是我的代码:

def buy_product(): 

global listbox, buy_product_price_entry
global s, ip,port, address,owners_ip, owners_port, username
address = (ip,int(port))

try:
    item = listbox.get(listbox.curselection())        


    price = int(buy_product_price_entry.get())
    highest_bid = int(item[5].replace('HIGHEST BID PRICE:', ''))
    K = item[0].replace('START:', '')
    end_time_str = item[1].replace('END:', '')
    L = datetime.strptime(end_time_str , '%H:%M:%S')
    current_time = datetime.strptime(datetime.now().strftime('%H:%M:%S'), '%H:%M:%S')


    if(price>highest_bid):
        if(current_time <= L):
            try: 
          
                A = int(item[2].replace('PRODUCT ID:', ''))
                B = item[3].replace('PRODUCT NAME:','')
                C = int(item[4].replace('BASE PRICE:', ''))
                D = int(buy_product_price_entry.get())
                E = username
                F = item[7].replace('SELLER:','')            
                G = item[8].replace('SELLER IP:','')
                H = int(item[9].replace('SELLER PORT:', ''))
                I = owners_ip
                J = owners_port
                
                tup = (A,B,C,D,E,F,G,H,I,J,K,end_time_str)
                s.sendto(str.encode(":::BUY_PRODUCT:::"  + json.dumps(tup)), address)
                buy_product_price_entry.delete(0, END)
            except:
                print("Unable to buy the product. Please try again.")
        else:
            messagebox.showinfo("Information", "Its now " + current_time.strftime('%H:%M:%S') + ", bidding time is over. " + item[6].replace('BIDDER:', '') +  " has won the bidding") 
    else:
        messagebox.showinfo("Information", "Inputted price is not greater than the highest bid price.")
except:
    messagebox.showinfo("Incomplete Input", "Please select first a product.") #Why does extra window occur?

当我退出 Tk 窗口时,消息框也消失了,但是当我退出消息框时,窗口仍然存在。我必须手动关闭它。似乎 Tk 窗口是消息框的父级。

【问题讨论】:

    标签: python python-3.x user-interface tkinter window


    【解决方案1】:

    当您创建一个消息框时,除非已经有一个窗口,否则它会自动创建一个。为了解决这个问题,您可以在主窗口上使用 .withdraw() 来隐藏它,例如

    from tkinter import *
    from tkinter import messagebox
    
    master = Tk()
    master.withdraw()
    messagebox.showinfo("Hi", "Hello World")
    

    【讨论】:

    • 谢谢。它解决了我的问题。我实际上还有另一个窗口。我只是没有在我的帖子中显示它,但它在另一个函数中被调用。为什么会这样?你能告诉我吗?谢谢。
    • @Eliyah 您可能想为此创建另一个问题,我不完全理解您的意思。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多