我是Python的新手,我的既定目标是尝试为自己创建一个应用程序以列出和处理一些数据;就我而言,有些硬币。 我是自学代码,但是还有很多我不了解的事情。
我会尽力组织代码,但是我已经达到了一些极限。
我的代码显示了一个Tk窗口,其中包含您在使用Toplevel的第二个窗口中输入的数据。当您使用ADD按钮时,它会显示该窗口,但是如果碰巧将其关闭并再次打开,则会显示错误。我肯定是因为我的窗口实例被破坏了,但是我很想得到您关于如何解决此问题的意见。您会提出什么建议?
感谢您的时间!
from tkinter import * # tkinter package import
from tkinter import ttk
window_size = '480x600+0+0'
############################ Window 1 configuration ############################
window = Tk()
window.title('Coin Collector') # title of the window
window.geometry(window_size) #taille de la fenetre
window.config(background='white')
#searchbar
search_bar_txt = Label(window, text='Search :')
search_bar_txt.place(height=40,width=70,x=1,y=1)
search_bar_entry = Entry(window)
search_bar_entry.place(height=40,width=410,x=71,y=1)
# tree view setup
tree=ttk.Treeview(window)
tree['columns']=('one','two','three')
tree.column('#0', width=29, minwidth=80)
tree.column('one', width=150, minwidth=80)
tree.column('two', width=150, minwidth=80)
tree.column('three', width=147, minwidth=80)
tree.heading('#0',text='#ID')
tree.heading('one', text='COIN')
tree.heading('two', text='YEAR')
tree.heading('three', text='PRICE')
tree.place(height= 510,x=0, y=50)
def show_window2():
window_2.deiconify()
###### BUTTONS ########
Button_add = Button(window, text='ADD', command=show_window2)
Button_add.place(height=40, width=120, x=0, y=560)
Button_remove = Button(window, text='REMOVE')
Button_remove.place(height=40, width=120, x=120, y=560)
Button_edit = Button(window, text='EDIT')
Button_edit.place(height=40, width=120, x=240, y=560)
Button_exit = Button(window, text='EXIT', command=exit)
Button_exit.place(height=40, width=120, x=360, y=560)
############################ Window 2 configuration ############################
window_2 = Toplevel()
window_2.title('Edit coin')
window_2.geometry('300x500+480+0')
window_2.config(background='white')
window_2.iconify()
window_2.protocol('WM_DELETE_WINDOW', window_2.destroy);#Changes close button
#coin type
coin_type_txt = Label(window_2, text='Coin Name :')
coin_type_txt.place(x=0, y=0)
coin_type_value = Entry(window_2)
coin_type_value.place(height=30, width=165, x=130, y=0)
#coin year
coin_year_txt = Label(window_2, text='Coin year :')
coin_year_txt.place(x=0, y=35)
coin_year_value = Entry(window_2)
coin_year_value.place(height=30, width=165, x=130, y=35)
#coin price
coin_price_txt = Label(window_2, text='Coin price :')
coin_price_txt.place(x=0, y=70)
coin_price_value = Entry(window_2,width=10)
coin_price_value.place(height=30, width=165, x=130, y=70)
def save_coin(): #create function to execute when clicked
tree.insert('','end',text='',values=(coin_type_value.get(),coin_year_value.get(),coin_price_value.get()))
#save coin
coin_save_button = Button(window_2, text='save', command=save_coin)
coin_save_button.place(height=40, width=300, x=0, y=460)
######FUNCTIONS######
def exit():
window.destroy()
window.mainloop()
答案 0 :(得分:0)
我这样做的方法是将顶层及其小部件的创建移到一个函数中。然后,您可以在显示窗口时检查该窗口是否存在。如果它由于用户销毁而不存在,则可以重新创建它。
例如,您的show_window
看起来像这样:
def show_window2():
if not window_2.winfo_exists():
create_window2()
window_2.deiconify()
答案 1 :(得分:0)
嘿,谢谢你的回答。工作正常!
我现在创建了一个Addcoin_window()函数,然后从位于后面几行的ADD按钮调用它。
有什么方法可以组织我的代码,以便我的所有功能都可以位于程序的末尾?只是我发现将函数Addcoin_window()放在我认为是我的主要窗口组织的位置上是“一团糟”。那是Python的方式吗?
再次感谢您的帮助。
from tkinter import * # tkinter package import
from tkinter import ttk
window_size = '480x600+0+0'
############################ Window 1 configuration ############################
window = Tk()
window.title('Coin Collector') # title of the window
window.geometry(window_size) #taille de la fenetre
window.config(background='white')
#searchbar
search_bar_txt = Label(window, text='Search :')
search_bar_txt.place(height=40,width=70,x=1,y=1)
search_bar_entry = Entry(window)
search_bar_entry.place(height=40,width=410,x=71,y=1)
# tree view setup
tree=ttk.Treeview(window)
tree['columns']=('one','two','three')
tree.column('#0', width=29, minwidth=80)
tree.column('one', width=150, minwidth=80)
tree.column('two', width=150, minwidth=80)
tree.column('three', width=147, minwidth=80)
tree.heading('#0',text='#ID')
tree.heading('one', text='COIN')
tree.heading('two', text='YEAR')
tree.heading('three', text='PRICE')
tree.place(height= 510,x=0, y=50)
def Addcoin_window():
window_2 = Toplevel()
window_2.title('Edit coin')
window_2.geometry('300x500+480+0')
window_2.config(background='white')
#window_2.iconify()
window_2.protocol('WM_DELETE_WINDOW', window_2.destroy);#Changes close button
#coin type
coin_type_txt = Label(window_2, text='Coin Name :')
coin_type_txt.place(x=0, y=0)
coin_type_value = Entry(window_2)
coin_type_value.place(height=30, width=165, x=130, y=0)
#coin year
coin_year_txt = Label(window_2, text='Coin year :')
coin_year_txt.place(x=0, y=35)
coin_year_value = Entry(window_2)
coin_year_value.place(height=30, width=165, x=130, y=35)
#coin price
coin_price_txt = Label(window_2, text='Coin price :')
coin_price_txt.place(x=0, y=70)
coin_price_value = Entry(window_2,width=10)
coin_price_value.place(height=30, width=165, x=130, y=70)
def save_coin(): #create function to execute when clicked
tree.insert('','end',text='',values=(coin_type_value.get(),coin_year_value.get(),coin_price_value.get()))
#save coin
coin_save_button = Button(window_2, text='SAVE', command=save_coin)
coin_save_button.place(height=40, width=300, x=0, y=460)
###### BUTTONS ########
Button_add = Button(window, text='ADD', command=Addcoin_window)
Button_add.place(height=40, width=120, x=0, y=560)
Button_remove = Button(window, text='REMOVE')
Button_remove.place(height=40, width=120, x=120, y=560)
Button_edit = Button(window, text='EDIT')
Button_edit.place(height=40, width=120, x=240, y=560)
Button_exit = Button(window, text='EXIT', command=exit)
Button_exit.place(height=40, width=120, x=360, y=560)
######FUNCTIONS######
def exit():
window.destroy()
window.mainloop()