2017-12-16 5 views
0

私は学校プロジェクトのGUIを使ってPCヘルパープログラムを作っています。Pythonでたくさんのtkinterボタンを作る方法

#All imports go here 
import tkinter as tk 
import os 

class Application(tk.Frame): 
    def __init__(self, master=None): 
     super().__init__(master) 
     self.pack() 
     self.create_widgets() 

    def create_widgets(self): 
     self.hi_there = tk.Button(self) 
     self.hi_there["text"] = "Hello, what would you like help with today?" 
     self.hi_there["command"] = self.say_hi 
     self.hi_there.pack(side="top") 

    def create_widgets(self): 
     self.notepad = tk.Button(self) 
     self.notepad["text"] = "Launch Notepad" 
     self.notepad["command"] = self.notepad 
     self.notepad.pack(side="right") 

    def create_widgets(self): 
     self.hi = tk.Button(self) 
     self.hi["text"] = "Launch CMD" 
     self.hi["command"] = self.cmd 
     self.hi.pack(side="left") 

     self.quit = tk.Button(self, text="QUIT", fg="red", 
          command=root.destroy) 
     self.quit.pack(side="bottom") 

    def cmd(self): 
     os.system("start cmd /a") 

    def notepad(self): 
     os.system("start notepad") 

root = tk.Tk() 
app = Application(master=root) 
app.mainloop() 

sys.exit() 
+1

2つ以上のボタンを追加しようとするとどうなりますか? – halfer

+0

@halferそれが見つからない場合は、[python-y.x]というタグ付きの質問を編集するときに、一般的なpythonタグを追加してください。ありがとう! –

+1

@Andras:そうする、はい - それを見つけようとするだろう ':)' – halfer

答えて

0

つ作成したボタンは、ここにdefindedされています:

def create_widgets(self): 
    self.hi = tk.Button(self) 
    self.hi["text"] = "Launch CMD" 
    self.hi["command"] = self.cmd 
    self.hi.pack(side="left") 

    self.quit = tk.Button(self, text="QUIT", fg="red", 
         command=root.destroy) 
    self.quit.pack(side="bottom") 

しかし、なぜここでしか

はコードです...私は2つのボタンが追加されているが、私は2つ以上のものを追加しているように見えるカントこれらは作成されていますか? 答えは簡単で、以下のコード内のコメントを見て:

class Application(tk.Frame): 
    def __init__(self, master=None): 
     super().__init__(master) 
     self.pack() 
     self.create_widgets() 
     self.create_widgets2() 
     self.create_widgets3() 

    def create_widgets(self): # define 
     self.hi_there = tk.Button(self) 
     self.hi_there["text"] = "Hello, what would you like help with today?" 
     self.hi_there["command"] = self.say_hi # additional bug (AttributeError: 'Application' object has no attribute 'say_hi'), change this to "self.hi_there["command"] = self.hi_there" 
     self.hi_there.pack(side="top") 

    def create_widgets(self): # re-define without usage 
     self.notepad = tk.Button(self) 
     self.notepad["text"] = "Launch Notepad" 
     self.notepad["command"] = self.notepad 
     self.notepad.pack(side="right") 

    def create_widgets(self): # again re-define without usage 
     self.hi = tk.Button(self) 
     self.hi["text"] = "Launch CMD" 
     self.hi["command"] = self.cmd 
     self.hi.pack(side="left") 

     self.quit = tk.Button(self, text="QUIT", fg="red", command=root.destroy) 
     self.quit.pack(side="bottom") 

    def cmd(self): 
     os.system("start cmd /a") 

    def notepad(self): 
     os.system("start notepad") 

このコードは以下のコードと同じEFECTを与えます。関数を定義する

class Application(tk.Frame): 
def __init__(self, master=None): 
    super().__init__(master) 
    self.pack() 
    self.create_widgets() 

def create_widgets(self): 
    self.hi = tk.Button(self) 
    self.hi["text"] = "Launch CMD" 
    self.hi["command"] = self.cmd 
    self.hi.pack(side="left") 

    self.quit = tk.Button(self, text="QUIT", fg="red", 
         command=root.destroy) 
    self.quit.pack(side="bottom") 

def cmd(self): 
    os.system("start cmd /a") 

def notepad(self): 
    os.system("start notepad") 

は、変数の値を割り当てることに似ています。

a = 1 
a = 4 
a = 150 
print(a) # will print 150, not 1, 4, 150 

ですから、同じ関数内で全てのボタン(あまりにも将来的には他のウィジェット)を入れたり、関数名を変更し、それらを呼び出す必要がありますinitファンクションにあります。

の作業コード(最初の方法で、私の意見では、より良い):

import tkinter as tk 
import os 


class Application(tk.Frame): 
    def __init__(self, master=None): 
     super().__init__(master) 
     self.pack() 
     self.create_widgets() 

    def create_widgets(self): 
     self.hi_there = tk.Button(self) 
     self.hi_there["text"] = "Hello, what would you like help with today?" 
     self.hi_there["command"] = self.hi_there 
     self.hi_there.pack(side="top") 

     self.notepad = tk.Button(self) 
     self.notepad["text"] = "Launch Notepad" 
     self.notepad["command"] = self.notepad 
     self.notepad.pack(side="right") 

     self.hi = tk.Button(self) 
     self.hi["text"] = "Launch CMD" 
     self.hi["command"] = self.cmd 
     self.hi.pack(side="left") 

     self.quit = tk.Button(self, text="QUIT", fg="red", command=root.destroy) 
     self.quit.pack(side="bottom") 

    def cmd(self): 
     os.system("start cmd /a") 

    def notepad(self): 
     os.system("start notepad") 


root = tk.Tk() 
app = Application(master=root) 
app.mainloop() 
sys.exit() 

第二の方法:私は、任意の言語のミスを謝るが、英語が母国語でない

import tkinter as tk 
import os 


class Application(tk.Frame): 
    def __init__(self, master=None): 
     super().__init__(master) 
     self.pack() 
     self.create_widgets() 
     self.create_widgets2() 
     self.create_widgets3() 

    def create_widgets(self): 
     self.hi_there = tk.Button(self) 
     self.hi_there["text"] = "Hello, what would you like help with today?" 
     self.hi_there["command"] = self.hi_there 
     self.hi_there.pack(side="top") 

    def create_widgets2(self): 
     self.notepad = tk.Button(self) 
     self.notepad["text"] = "Launch Notepad" 
     self.notepad["command"] = self.notepad 
     self.notepad.pack(side="right") 

    def create_widgets3(self): 
     self.hi = tk.Button(self) 
     self.hi["text"] = "Launch CMD" 
     self.hi["command"] = self.cmd 
     self.hi.pack(side="left") 

     self.quit = tk.Button(self, text="QUIT", fg="red", command=root.destroy) 
     self.quit.pack(side="bottom") 

    def cmd(self): 
     os.system("start cmd /a") 

    def notepad(self): 
     os.system("start notepad") 


root = tk.Tk() 
app = Application(master=root) 
app.mainloop() 
sys.exit() 

関連する問題