2017-05-15 8 views
-1

私はPythonにはかなり新しく、昨日Tkinterを学び始めました。私は銀行システムを作成しており、ボタンからメニューを作りました。私が持っている問題は、ボタンをクリックしたときに別のウィンドウを開く方法がわからないことです。私はtop=Toplevel()で試しましたが、お互いの上に2つのウィンドウしか開きません。私が必要とするのは、追加のウィンドウがボタン(イベント)でアクティブになっているときだけ開くことです。私は約6つのボタンがあるので、私は本当に立ち往生しているので、誰かが私を助けることができますか?Tkinter with Python

これまでの私のコードは次のとおりです。

root.minsize(width=400, height=400) 
root.maxsize(width=400, height=400) 
root.configure(background='#666666') 
label = Frame(root).pack() 
Lb = Label(label, text='Welcome to Main Menu',bg='#e6e6e6', width=400).pack() 

menu = Frame(root,).pack() 
btn_1 = Button(menu, text='Make Deposit', width=15, height=2).pack(pady=5) 
btn_2 = Button(menu, text='Withdrawal', width=15, height=2).pack(pady=5) 
btn_3 = Button(menu, text='Accounts', width=15, height=2).pack(pady=5) 
btn_4 = Button(menu, text='Balance', width=15 ,height=2).pack(pady=5) 
btn_5 = Button(menu, text='Exit', width=15, height=2).pack(pady=5) 
root.mainloop() 

は事前

+0

ボタンの 'command'オプションにToplevelを開く関数を渡す必要があります。 –

答えて

0

で助けてくれてありがとう多分これは仕事ができる:

from Tkinter import * 

class firstwindow: 
    def __init__(self): 
     #Variables 
     self.root= Tk() 
     self.switchbool=False 
     #Widgets 
     self.b = Button(self.root,text="goto second window",command= self.switch) 
     self.b.grid() 
     self.b2 = Button(self.root,text="close",command= self.root.quit) 
     self.b2.grid() 
     self.root.mainloop() 

    #Function to chage window 
    def switch(self): 
     self.switchbool=True 
     self.root.quit() 
     self.root.destroy() 

class secondwindow: 
    def __init__(self): 
     self.root= Tk() 
     self.b2 = Button(self.root,text="close",command= self.root.quit) 
     self.b2.grid() 
     self.root.mainloop() 

one = firstwindow() 

if one.switchbool: 
    two = secondwindow() 

Tkinter referenceに見てください。すべての普遍的なメソッドとウィジェットのメソッドをチェックしてください。あなたは何でもすることができます。

0

以下は、ボタンをクリックしてウィンドウを開く方法の小さな例です。ユーザーがボタンをクリックすると、ボタンのcommandオプションに渡された関数open_windowが呼び出されます。

import tkinter as tk 

def open_window(): 
    top = tk.Toplevel(root) 
    tk.Button(top, text='Quit', command=top.destroy).pack(side='bottom') 
    top.geometry('200x200') 

root = tk.Tk() 

tk.Button(root, text='Open', command=open_window).pack() 
tk.Button(root, text='Quit', command=root.destroy).pack() 

root.mainloop()