2012-04-16 23 views
0

+ + _ + + + _ + + + _ + + + _ + + + _ + + + _ + ++兩個窗口在python,嚇到我了

**沒關係! **這個問題得到解答!它只是需要這個底部,而不是該根beezwax ...

燁。這是底部。它只需要一點代碼清理:)....沒關係。不管怎麼說,還是要謝謝你!

#omit root 
app = App() 
app.mainloop() 

+ + _ + + + _ + + + _ + + + _ + + + _ + + + _ + + ++

我確實努力嘗試過了兩個小時,以擺脫這種第二窗口。我實現了彈出後索要的文件夾的名字,我突然有了程序運行時會打開兩個窗口。我只想要一個,這真的很讓人煩惱。我想讓這個程序用四個按鈕打開一個窗口,並在上面有一個標題。其中一個按鈕要求輸入,然後消失。有人能幫我回到一個窗口嗎?謝謝。我爲這個混亂的代碼道歉,這是我的第一個這種風格的腳本。

#!/usr/bin/python 
      #from Tkinter import * 
      import Tkinter as tk 

      import os 
      import tkFileDialog 

      class App(tk.Tk): 
      #class App: 

       #def __init__(self, master): 
       def __init__(self): 
       tk.Tk.__init__(self) 


       frame = tk.Frame(self) 
       frame.pack() 

       self.button = tk.Button(frame, text="leave", fg="red", command=frame.quit) 
       self.button.pack(side=tk.LEFT) 

       self.fetch = tk.Button(frame,text="Choose Folder for Pictures",fg="salmon",command=self.choose) 
       self.fetch.pack(side=tk.LEFT) 

       self.fetch = tk.Button(frame,text="Name folder on site (public)",command=self.on_click) 
       self.fetch.pack(side=tk.LEFT) 

       self.hi_there = tk.Button(frame, text="Create code for images", fg="brown", command=self.generate) 
       self.hi_there.pack(side=tk.LEFT) 
       #oroville dam is the highest in the country 
       w = tk.Label(text="MSP Art File Fetcher") 
       w.pack() 
      # Toplevel window 

       top = self.top = tk.Toplevel(self) 
       myLabel = tk.Label(top, text='Name of image directory:') 
       myLabel.pack() 

       self.myEntryBox = tk.Entry(top) 
       self.myEntryBox.pack() 

       mySubmitButton = tk.Button(top, text='Done', command=self.submit_name) 
       mySubmitButton.pack() 

       top.protocol("WM_DELETE_WINDOW", self.save_top) 

       top.withdraw() 



       def save_top(self): 
       self.top.withdraw() 


       def choose(self): 
       self.foldername=tkFileDialog.askdirectory() 
       print self.foldername 


       def name(self): 
       print self.foldername 

       def generate(self): 
       print self.foldername 
        self.path=self.foldername # failing, works 
       self.dirlist=os.listdir(self.path) 
       yoz = file('demcode.txt','wt')#totally works 
       f=open('demcode.txt', 'r+') 
       f.write(self.foldername) 
       f.write('\ndo not be a crack addic\n') 
       f.write('\n') 
       print self.dirlist 
       print self.dirlist[0] 
       self.y=len(self.dirlist) 
       print self.y 
       for x in range(0,self.y): #works great 
       #for x in range(0,4):#self.y: #failing 
        print 'We\'re on image %d' % (x) 
       #print in self.dirlist 
       f.write('\n'.join(self.dirlist[0]))#returns a vertical word!? 
       f.write('\n') 
       f.write(self.dirlist[0]) 
       f.write('\n') 
       f.write('\n') 
       f.write('\n') 
       f.write(', '.join(self.dirlist))#CAUTION, will write over existing data 

       def say_hi(self): 
       print "don't be a crack addic" 

       def submit_name(self): 
       if self.myEntryBox.get() != "": 
        self.username = self.myEntryBox.get() 
        self.myEntryBox.delete(0, 'end') 
        self.top.withdraw() 

       def on_click(self): 
       self.top.deiconify() 
      """ 
       def show_name(self): 
       self.mainText.delete('1.0', 'end') 
       self.mainText.insert('end', 'Welcome ' + self.username + '!') 
      """ 
      root = tk.Tk() 

      app = App() 

      root.mainloop() 
      """ 
      """ 
      achieve this format of html. python program will loop every file in the directory, placing the name of the file in the set path (asked directory name), and write the approiate code for lightbox 
      """ 
      <h1>MSP (Acorns) Gallery</h1> 
      #http://www.mspart.com/lightbox.html 
      <div id="page"> 
      <div id="images"> 
      <ul class="gallery"> 
       <a href="images/Pastels/alpha_farm.jpg" rel="lightbox"> </a> 
       <li><a href="images/Pastels/alpha_farm.jpg" rel="lightbox"><img src="images/Pastels/alpha_farm.jpg" alt="description"></a></li> 
       <a href="images/Pastels/_day_island.jpg" rel="lightbox"> </a> 
       <li><a href="images/Pastels/_day_island.jpg" rel="lightbox"><img src="images/Pastels/_day_island.jpg" alt="description"></a> 
      </li></ul> 
      </div> 
      </div> 
+6

如果你找到了解決辦法,你應該張貼它作爲一個答案,[標記爲接受(http://meta.stackexchange.com/questions/5234)。 – 2012-04-16 04:01:02

+0

是的,我只是讓我的個人資料。有一個奇怪的八小時溢出阻止我添加一個答案或標記爲解決... – 2012-04-16 06:27:05

回答

0

在您的app類中,您已經定義了Tk。所以,當你定義一個app和另一Tk,要創建兩個窗口。刪除一個或另一個應該只創建一個窗口。