Python Label problemmer
Hej EksperterJeg har et lille problem med mine python kode.
Når jeg starter med et vindue1 og starter vindue2 så kommer labelen fra vindue2 frem på vindue1. Hvad gør jeg forkerte ?
Kode på vindu1:
#!/usr/bin/env python
import Tkinter
import classvindue2
class vindue1(Tkinter.Tk):
MyWidth = 370
MyHeight = 170
myTitel = 'vindue1'
def __init__(self, parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
reload(classvindue2)
self.gui()
def gui(self):
self.config(width=self.MyWidth,height=self.MyHeight)
self.selfcenter()
self.title(self.myTitel)
button = Tkinter.Button(self,text = "Start 2. vindue", command = self.OnVindueClick);
button.place(x=30,y=20,width=120,height=20)
w2 = Tkinter.Label(text = "Test Label",relief='solid')
w2.place(x=30,y=55,width=120,height=20)
def selfcenter(self):
self.update_idletasks()
sw = self.winfo_screenwidth()
sh = self.winfo_screenheight()
w = self.winfo_reqwidth()
h = self.winfo_reqheight()
newGeometry='+%d+%d' % ((sw/2)-(w/2), (sh/2)-(h/2))
self.geometry(newGeometry=newGeometry)
def OnVindueClick(self):
app = classvindue2.vindue2(None)
app.mainloop()
if __name__ == "__main__":
mainapp = vindue1(None)
mainapp.mainloop(0)
Kode på vindue2:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Tkinter
import tkFont
class vindue2(Tkinter.Tk):
MyWidth = 370
MyHeight = 170
myTitel = 'Vindue2'
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self._gui_()
def centerx(self,xwidth):
return (self.MyWidth/2) - (xwidth/2)
def getFontsize(self, cFont, sstr): #return (w,h)
return (cFont.measure(sstr),cFont.metrics("linespace"))
def selfcenter(self):
self.update_idletasks()
sw = self.winfo_screenwidth()
sh = self.winfo_screenheight()
w = self.winfo_reqwidth()
h = self.winfo_reqheight()
newGeometry='+%d+%d' % ((sw/2)-(w/2), (sh/2)-(h/2))
self.geometry(newGeometry=newGeometry)
def _gui_(self):
self.customFont = tkFont.Font(size=36)
sstr = u"Test"
(w,h) = self.getFontsize(self.customFont,sstr)
w1 = Tkinter.Label(text = sstr,font=self.customFont,relief='solid')
w1.place(x=self.centerx(w),y=15,width=w+2,height=h+2)
self.config(width=self.MyWidth,height=self.MyHeight)
self.title(self.myTitel)
self.selfcenter()
if __name__ == "__main__":
appmain = vindue2(None)
appmain.mainloop(0)