Python New TKinter Window Appears when MessageBox is Shown

…衆ロ難τιáo~ 提交于 2020-06-23 09:58:31

问题


When displaying a messagebox, a new Tkinter window always pops up. Why does this happen? I didn't create a new window. How do I get rid of the window?

Below is my code:

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?

When I exit the Tk window, the messagebox also disappears but when I exit the messagebox, the window remains. I have to close it manually. It seems like the Tk window is a parent of the messagebox.


回答1:


When you create a messagebox, unless there is a window already, it creates one automatically. To get around this, you can use .withdraw() on your master window to hide it, something like

from tkinter import *
from tkinter import messagebox

master = Tk()
master.withdraw()
messagebox.showinfo("Hi", "Hello World")


来源:https://stackoverflow.com/questions/53240256/python-new-tkinter-window-appears-when-messagebox-is-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!