2013-05-25 23 views
0

我有一個程序寫在IDLE3.3和tkinter,我不知道在哪裏放置主循環()。該程序會創建一個系統托盤圖標,如果您在上下文菜單中單擊「新筆記」,則會創建一個小記錄。如果在註釋結尾處有「self.root.mainloop()」行。 init(),顯示註釋,但只顯示一個註釋。如果我創建第二個,第一個音符就死了,沒有進一步發生。 但是,如果我不調用init-method中的mainloop(),我會看到有幾個筆記被創建,因爲它被打印在shell中。 所以問題是,我應該在哪裏放置主循環,以便每個新創建的notw都可以顯示並運行?對不起,這可能是愚蠢的問題,但我無法弄清楚。我應該在哪裏放置主循環?

from tkinter import * 
import sys 
from PyQt4.QtGui import * 
import threading 


class Note(): 

    yellow=["#e7e37c","#d9d574"] 

    def __init__(self,noteset=None, properties=None): 
     self.root=Tk() 
     self.noteset=noteset 
     self.properties=properties 
     self.screen_width = self.root.winfo_screenwidth()  
     self.screen_height = self.root.winfo_screenheight()  
     print("No initial properties to load => creating new note") 
     self.notecolor=self.yellow[0] 
     self.gripcolor=self.yellow[1] 
     self.root.overrideredirect(1) 
     self.text="" 
     self.font="arial" 
     self.fontsize=10 
     self.sizeX=250 
     self.sizeY=200 
     self.posX=int(self.screen_width/2 - self.sizeX/2) 
     self.posY=int(self.screen_height/2 - self.sizeY/2) 
     self.root.wm_geometry("%sx%s+%s+%s" %(self.sizeX, self.sizeY, self.posX, self.posY))   
     self.root.wm_attributes("-topmost",1) 
     self.GUI() 
     self.bindings() 

     self.root.mainloop() 

    def bindings(self): 
     self.frmGRIP.bind("<ButtonPress-1>", self.StartMove) 
     self.frmGRIP.bind("<ButtonRelease-1>", self.StopMove) 
     self.frmGRIP.bind("<B1-Motion>", self.OnMotion) 

    def StartMove(self, event): 
     self.startx = event.x 
     self.starty = event.y 
    def OnMotion(self, event):   
     mousex,mousey=self.root.winfo_pointerxy() 
     self.root.geometry("+%s+%s" % (mousex-self.startx, mousey-self.starty)) 
    def StopMove(self, event): 
     self.posX = self.root.winfo_x() 
     self.posY = self.root.winfo_y() 

    def GUI(self):     
     self.frmTOP=Frame(master=self.root,height=15) 
     self.frmBOTTOM=Frame(master=self.root,width=300,height=300) 
     self.frmGRIP=Frame(self.frmTOP,bg=self.gripcolor,height=15) 
     self.frmRESIZE=Frame(self.frmBOTTOM,width=300,height=10) 
     self.frmTEXT=Frame(self.frmBOTTOM,bg=self.notecolor,width=300,height=300) 
     self.frmRESIZE_empty=Frame(self.frmRESIZE,bg=self.notecolor,height=10) 
     self.frmRESIZE_grip=Frame(self.frmRESIZE,bg=self.gripcolor,width=10,height=10) 

     self.frmTOP.pack(fill=X,expand=NO) 
     self.frmBOTTOM.pack(side=BOTTOM,fill=BOTH,expand=YES) 
     self.frmGRIP.pack(side=LEFT,fill=X,expand=YES) 
     self.frmRESIZE.pack(side=BOTTOM,fill=X)   
     self.frmTEXT.pack(side=BOTTOM,fill=BOTH,expand=YES) 
     self.frmRESIZE_empty.pack(side=LEFT,fill=X,expand=YES) 
     self.frmRESIZE_grip.pack(side=LEFT,expand=NO) 

     self.T=Text(self.frmTEXT, 
        height=6,width=30, 
        bd=0,wrap=WORD,pady=3,padx=5, 
        bg=self.notecolor,undo=1, 
        font=(self.font,self.fontsize) 
        ) 
     self.T.insert(END,self.text) 
     self.T.pack(fill=BOTH,expand=YES)   

class Noteset(): 
    def __init__(self): 
     self.notes = [] 
    def newNote(self): 
     note=Note(noteset=self) 
     self.notes.append(note) 
     print(self.notes) 
     return note 

class Main(): 
    def __init__(self): 
     self.N=Noteset() 
     app = QApplication(sys.argv) 

     trayIcon = QSystemTrayIcon(QIcon("J:\\python\\SimpleNotes.ico"), app) 
     menu = QMenu() 

     ActionNewNote = menu.addAction("new Note") 
     ActionNewNote.triggered.connect(self.newNote) 
     trayIcon.setContextMenu(menu) 
     trayIcon.show() 

     app.exec() 

    def newNote(self): 
     self.N.newNote() 


Main() 

回答

1

你不能成功地一起使用Qt和Tkinter。另外,如果從上面刪除Qt,則還有另外一個問題,即不應創建多於Tk的實例。

要回答您的具體問題,mainloop通常是您執行的最後一行代碼。由於它是一個無限循環,因此在調用mainloop之後的任何代碼都不會執行,直到主窗口被銷燬。

一個Tkinter的應用程序的正常結構是這樣的:

import Tkinter as tk 
class MyApp(...): 
    def __init__(self, root, ...): 
     ... 
    ... 

root = tk.Tk() 
myApp(root) 
root.mainloop() 
+0

是否有可能有隻用一個TK()打開多個窗口?如果root是父級,則所有新的Notes(= MyApps)都將位於Tk-Window內部,這並非意圖。我想創建一些類似於Windows 7的短信......可能是我誤解了某些東西。 – user2366975

+0

@ user2366975:是的。如果你想要其他窗口,創建'Toplevel'類的實例。 –

1

它不是一個好主意,因爲每個mainloops塊相互混合GUI框架,你最好在一個或另一個編寫整個事情。