2013-05-25 3 views
0

은 내가)합니다 (주회 돌이를 배치 할 위치를 모르는 IDLE3.3과 Tkinter로 작성된 프로그램이 있습니다. 상황에 맞는 메뉴에서 "새 메모"를 클릭하면 작은 메모를 만드는 시스템 트레이 아이콘이 생성됩니다. Note 끝에 "self.root.mainloop()"행이있는 경우. 초기화(), 메모가 표시하지만, 단 하나의 참고한다. 두 번째 음표를 만들면 첫 번째 음표는 죽고 더 이상 발생하지 않습니다. 내가 초기화 메소드에서 주회 돌이()를 호출하지 않는 경우에, 나는 그것이 쉘에 인쇄되어 있기 때문에 거기에 몇 가지 메모를 만든 것을 알 수있다. 질문은, 새로이 만들어진 모든 노트가 보여지고 작동 할 수 있도록 메인 루프를 어디에 두어야합니까? 그 어리석은 질문에 대해 미안하지만 그것을 이해할 수는 없습니다. mainloops 각각 서로 블록으로메인 루프는 어디에 두어야합니까?

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을 호출 한 후 코드는 주 창이 소멸 될 때까지 실행되지 않습니다.

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

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

단지 하나의 Tk()으로 열린 여러 개의 창을 가질 수 있습니다 :

Tkinter를 응용 프로그램의 일반적인 구조는 다음과 같이 간다? 루트가 부모 인 경우, 모든 새로운 Notes (= MyApps)는 의도하지 않은 Tk-Window 내부에있게됩니다. 나는 창 7의 그 짧은 메모 같은 것을 만들고 싶다. 아마 나는 sth를 오해하고있다. – user2366975

+0

@ user2366975 : 예. 다른 창을 원하면'Toplevel' 클래스의 인스턴스를 생성하십시오. –

1

그것의 좋은 생각이 GUI 프레임 워크를 믹스하려면 더 나을 둘 중 하나에 모든 일을 코드.

관련 문제