Python OptionMenus 不断消失和重新出现 - 我怎样才能让它们“留下来”?

发布于 2025-01-08 06:22:35 字数 3841 浏览 0 评论 0原文

我有一个简单的学校作业要做 - F、C 和 K 之间的转换器。 我在使用选项菜单时遇到问题,因为当我用鼠标移动时,它们不断消失并重新出现。例如,如果我从 OptionMenu2 中选择,OptionMenu1 将消失。怎样才能让他们“留下来”呢?

代码包含2张图片。如果您想运行代码,则需要一个 for 按钮。您也许可以删除上面的一个:)

我将不胜感激任何对此的帮助!

# -*- 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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事↓在人 2025-01-15 06:22:35

当您使用标签作为其他小部件的容器时,您正在做一些非常不寻常的事情。该标签不适合用作其他小部件的容器。虽然这应该是允许的,但它显然导致了您所看到的行为。如果您将 panel1 和 panel2 更改为框架而不是标签,您的问题就会消失。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文