2010-11-14 39 views
1

嗨我一直在試圖獲得一個簡單的GUI工作,它接受一個字符串使用Tkinter模塊中包含的條目表單,並根據它在哪個容器中運行「killall +字符串「或」打開+字符串「通過終端。以下是我正在使用的代碼。問題在第8行,上面的註釋更詳細地解釋了什麼是錯誤的。在使用Python的Tkinter中的多個幀

from tkinter import * 
import os 
root = Tk() 
def buttonPressed(): 
    # this is the problem in the program. 
    # it keeps returning [''] in the print statement and doesn't quit 
    # any applications 
    listOfApps = form1.form.get().split(',') 
    # the print is just so i can see the output 
    print(listOfApps) 
    # goes through each item in list and kills them(yes i know there are much easier 
    # ways to do this, i'm just trying to learn a bit about GUI and the os module 
    for i in listOfApps: 
     try: 
      os.system("killall " + i) 
     except: 
      pass 
def buttonPressed2(): 
    filesToOpen = form2.form.get().split(', ') 
    for i in filesToOpen: 
     try: 
      os.system("open " + i) 
     except: 
      pass 
class form_and_submit: 
    def _init_(self): 
     pass 
    #creates an instance of a form with seperate variables for button text, buton command, and color of the frame 
    def create_container(self, buttonText, buttonCommand, color): 
     #Creating all widgets/containers 
     self.container = Frame(root) 
     self.form = Entry(self.container) 
     self.button = Button(self.container) 
     #defining variables for all widgets/containers 
     root['background'] = color 
     self.container['background'] = color 
     self.button['text'] = buttonText 
     self.button['command'] = buttonCommand 
     self.form['background'] = color 
     self.form['border'] = '5' 
     self.form['highlightthickness'] = '0' 
     #Packing all widgets/containers 
     self.container.pack() 
     self.form.pack() 
     self.button.pack() 
#creating forms and putting them in root with desire attributes 
form1 = form_and_submit 
form2 = form_and_submit 
form1.create_container(form1, 'kill matching processes', buttonPressed, 'red') 
form2.create_container(form2, 'Open desired files', buttonPressed2, 'blue') 
#starts up window 
root.mainloop() 

我相信這裏的問題是多個幀中,因爲它與前細的工作只是爲Form1的類form_and_submit的一個實例。提前感謝任何能夠幫助解決這個問題的人。

+0

1)。不要使用'os.system'。使用'subprocess'。 2)。它是'__init__'(兩個下劃線),而不是'_init_'。 – user225312 2010-11-14 18:53:46

+0

感謝您的回答,難怪我無法檢索類變量,這是因爲我沒有使用帶有兩個下劃線的__init__,這就是爲什麼我的課非常混亂。有一點改變了我的代碼,它工作得很好,再次感謝。 – mbuffett 2010-11-15 13:40:44

回答

0

這可能不是你要尋找的答案,但我可以把它通過擺脫類的做正確的事。以下是我有:

from tkinter import * 
import os 
root = Tk() 

def buttonPressed(): 
    # this is the problem in the program. 
    # it keeps returning [''] in the print statement and doesn't quit 
    # any applications 
    listOfApps = form.get().split(',') 
    # the print is just so i can see the output 
    print(listOfApps) 
    # goes through each item in list and kills them(yes i know there are much easier 
    # ways to do this, i'm just trying to learn a bit about GUI and the os module 
    for i in listOfApps: 
     try: 
      os.system("killall " + i) 
     except: 
      pass 
def buttonPressed2(): 
    filesToOpen = form2.get().split(', ') 
    for i in filesToOpen: 
     try: 
      os.system("open " + i) 
     except: 
      pass 

container = Frame(root) 
form = Entry(container) 
button = Button(container) 
root['background'] = 'red' 
container['background'] = 'red' 
button['text'] = 'kill matching processes' 
button['command'] = buttonPressed 
form['background'] = 'red' 
form['border'] = '5' 
form['highlightthickness'] = '0' 


container2 = Frame(root) 
form2 = Entry(container2) 
button2 = Button(container2) 
root['background'] = 'blue' 
container2['background'] = 'blue' 
button2['text'] = 'Open desired files' 
button2['command'] = buttonPressed2 
form2['background'] = 'blue' 
form2['border'] = '5' 
form2['highlightthickness'] = '0' 

container.pack() 
form.pack() 
button.pack() 

container2.pack() 
form2.pack() 
button2.pack() 

#starts up window 
root.mainloop() 
+0

感謝您的回答,但是我使用類的原因是爲了能夠學習它的語法,我意識到如果沒有類,它可以變得容易得多,但這只是一個幫助我學習的練習程序。 – mbuffett 2010-11-15 13:44:12