Question

I have a simple school assignment to do - a converter between F, C and K. I'm having problems with OptionMenus, because they keep disappearing and reappearing when I move over with the mouse. For example if I choose from OptionMenu2, OptionMenu1 will disappear. How can I make them "stay"?

Code includes 2 pictures. If you want to run the code, you'll need one for button. You can probably just delete the one on top :)

I'll appreciate any help on this!

# -*- coding: cp1250 -*- from Tkinter import * import tkMessageBox import tkFont from array import * from decimal import Decimal class MojGUI(Frame): def __init__(self, master=None): Frame.__init__(self, master) ##NASLOV image1 = PhotoImage(file="naslov.gif") panel1 = Label(root, image=image1,background="#FFFFFF",height=50,width=400) panel1.pack(side=TOP) panel1.image = image1 panel2 = Label(root,background="#FFFFFF") panel2.pack(side=TOP) self.vnesibesedilofont = tkFont.Font(family="Verdana",size=16,weight="bold") ##VPIS CIFRE self.entryfont = tkFont.Font(family="Verdana",size=14,weight="bold") self.entryWidget = Entry(panel2, width="4",font=self.entryfont,foreground="#FFFFFF", background="#bb0000") self.entryWidget.pack(side=LEFT) ##ENOTA1 self.text1 = StringVar(master) self.text1.set("C") # default value self.enota1 = OptionMenu(panel2, self.text1, "C", "F", "K") self.enota1.pack(side=LEFT) ##ENACAJ self.enacaj = tkFont.Font(family="Verdana",size=16,weight="bold") self.znak = StringVar() self.znak.set(" = ") self.entryLabel = Label(panel2,textvariable=self.znak, background='#FFFFFF',font=self.enacaj) self.entryLabel.pack(side=LEFT) ##VREDNOST self.textvrednost = StringVar() self.vredno = tkFont.Font(family="Verdana",size=14,weight="bold") self.vrednost = Label(panel2,textvariable=self.textvrednost, width="9", foreground='#000000',background='#FFFFFF',font=self.vredno) self.vrednost.pack(side=LEFT) self.textvrednost.set("") ##ENOTA2 self.text2 = StringVar(panel2) self.text2.set("C") # default value self.enota2 = OptionMenu(panel2, self.text2, "C", "F", "K") self.enota2.pack(side=LEFT) ##GUMB image2 = PhotoImage(file="pretvori.gif") entryButton = Button(panel2,text="",bd="0",cursor="hand2",background='#FFFFFF',activebackground="#FFFFFF",command=self.pretvori,image=image2) entryButton.pack(side=LEFT) entryButton.image = image2 self.pack() def pretvori(self): enota1=self.text1.get() enota2=self.text2.get() original=Decimal(self.entryWidget.get()) rezultat= StringVar() rezultat.set(str(original)) if (enota1 == "C") &(enota2 == "K"): rezultat.set(str(round(original+273,2))) if (enota2 == "C") &(enota1 == "K"): rezultat.set(str(round(original-273,2))) if (enota1 == "K") &(enota2 == "F"): rezultat.set(str(round( Decimal(original-273) * Decimal(1.8)+32 ,2))) if (enota2 == "K") &(enota1 == "F"): rezultat.set(str(round( Decimal(original-32) / Decimal(1.8)+273 ,2))) if (enota1 == "C") &(enota2 == "F"): rezultat.set(str(round(original*Decimal(1.8)+32,2))) if (enota2 == "C") &(enota1 == "F"): rezultat.set(str(round((original-32)/Decimal(1.8),2))) self.textvrednost.set(rezultat.get()) self.znak.set(" = ") root = Tk() root.title('Pretvornik') root.wm_minsize(500, 200) root.wm_resizable(0, 0) w = root.winfo_screenwidth() h = root.winfo_screenheight() rootsize = tuple(int(_) for _ in root.geometry().split('+')[0].split('x')) x = (w - 500)/2 y = (h - 200)/2 root.geometry("%dx%d+%d+%d" % (rootsize + (x, y))) root.config(background="#FFFFFF") app = MojGUI(master=root) root.mainloop()

Was it helpful?

Solution

You are doing something very unusual when you use a label as a container for other widgets. The label is not an appropriate widget to use as a container for other widgets. While this should be allowable, it apparently is causing the behavior that you are seeing. If you change panel1 and panel2 to be frames rather than labels, your problem goes away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top