2017-12-16 12 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

두 개 이상의 버튼을 추가하려고하면 어떻게됩니까? – halfer

+0

@halfer 만약 당신이 빠진 것을 알아 채 셨다면 [python-y.x] 태그가 달린 질문을 편집 할 때 일반 파이썬 태그를 추가하십시오. 감사! –

+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") 

왜 여기 만

코드입니다 ... 나는 두 개의 버튼을 추가하지만이보다 더 추가하는 것 캔트 이것들은 만들어 졌는가? 대답은 간단, 아래 코드에서 주석을 찾습니다

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에도 있습니다.

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

그래서 같은 기능에 (너무 미래에 다른 위젯) 모든 버튼을 넣어 또는 함수 이름을 변경하고 그들에게 전화해야

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") 

정의 함수는 변수 값을 할당과 유사 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() 

.