【问题标题】:Two windows in python, freaking me outpython中的两个窗口,吓到我了
【发布时间】:2012-04-16 03:15:02
【问题描述】:

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

** 没关系! ** 这个问题已经回答了!它只需要在底部而不是根蜂蜡......

是的。这是底部。它只需要一点代码清理:)....没关系。还是谢谢!

#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>

【问题讨论】:

  • 如果您找到了解决方案,您应该将其作为答案发布并mark it as accepted
  • 是的,我刚刚创建了我的个人资料。溢出有一个奇怪的八小时阻止我添加答案或将其标记为已解决...

标签: python windows tkinter


【解决方案1】:

在您的app 类中,您已经定义了一个Tk。因此,当您定义一个app 和另一个Tk 时,您将创建两个窗口。删除一个或另一个应该只创建一个窗口。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 2020-01-04
    • 1970-01-01
    • 2019-10-07
    • 2021-05-21
    相关资源
    最近更新 更多