2016-04-22 14 views
0

このMainFrameクラスでは、クリックするとクラスを新しいウィンドウにインポートする4つのボタンがあります(Updateを除く)。私は各ボタンを一度クリックすることができますが、もう一度ボタンをクリックしようとすると、新しいウィンドウはポップアップされず、何も起こりません。なぜ各ボタンは一度しか動作しませんか?Tkinterボタンは一度しか動作しません

from tkinter import * 
import tkinter.messagebox as tm 
import Users 
import re 
import sqlite3 

db = sqlite3.connect('game nebula.db') 
c = db.cursor() 


class MainFrame(Frame): 
    def __init__(self, master): 
     Frame.__init__(self, master) 

     self.master = master 
     self.mainUI() 

    def mainUI(self): 

     self.master.title("Games Nebula") 
     self.pack(fill=BOTH, expand=True) 

     frame1 = Frame(self) 
     frame1.pack(fill=X) 
     frame2 = Frame(self) 
     frame2.pack(fill=X) 
     frame3 = Frame(self) 
     frame3.pack(fill=X) 

     self.label_1 = Label(frame1, text="Welcome to Games Nebula!", fg='green', relief='groove', width='40') 
     self.label_1.pack(side=TOP, padx=5, pady=5) 

     self.searchbtn = Button(frame2, text="Browse Games", fg='green', command = self._search_btn_clicked) 
     self.searchbtn.pack(side=LEFT, padx=25, pady=10) 
     self.addbtn = Button(frame2, text="Add games", fg='green', command=self._addbtn_btn_clicked) 
     self.addbtn.pack(side=RIGHT, padx=25, pady=10) 
     self.updatebtn = Button(frame3, text="Update", fg='green', command=self._update_btn_clicked) 
     self.updatebtn.pack(side=LEFT, padx=40, pady=10) 
     self.deletebtn = Button(frame3, text="Delete Games", fg='green', command=self._delete_btn_clicked) 
     self.deletebtn.pack(side=RIGHT, padx=25, pady=10) 


    def _search_btn_clicked(self): 
     print("Searching") 
     import GameSearch 


    def _addbtn_btn_clicked(self): 
     import Add 

    def _update_btn_clicked(self): 
     print("Updating") 

    def _delete_btn_clicked(self): 
     import Delete 


root = Tk() 
root.geometry("300x200+300+300") 
lf = MainFrame(root) 
root.mainloop() 

答えて

1

モジュールは一度しか輸入されているため、次のモジュールがインポートされたとき、Pythonはそれを参照して、再度インポートされません。

それは、のでそれをを悪い習慣をしないされていますが、再インポートを強制したい場合、あなたは次のように行うことができます:あなたがしたい理由は、私が失敗し

import importlib 

importlib.reload(module_name) # attention: the module must have been 
           #   imported first for this to work 

ボタンをクリックするだけでモジュールをインポート(および再インポート)できます。

0

あなたが実際にやりたいことは、各モジュールでいくつかのコードを繰り返し実行することです。 AS RBが説明したように、モジュールを繰り返しインポートすることはそうしません。あなたがすべきことは、インポートした後に呼び出すことができるモジュール内の関数とクラスを定義することです。それ以外の場合は、mainという関数内のすべてをラップし、「追加」ボタンを定義するときはcommand=Add.mainなどを使用します。

モジュールのテストは、オブジェクトを定義してから名前にバインドするだけで、実行時間の長い計算やウィンドウを開くなどの副作用がなくても簡単です。

関連する問題