Python OptionMenus不断消失和重新出现我如何才能让他们“留下来”?

2024-05-20 23:33:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的学校作业要做-转换F,C和K。 我对OptionMenus有问题,因为当我用鼠标移动时,它们总是消失和重新出现。例如,如果我从OptionMenu2中选择,OptionMenu1将消失。我怎样才能让他们“留下来”?在

代码包括2张图片。如果你想运行代码,你需要一个按钮。你可以删除上面的一个:)

我会很感激你的帮助!在

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


Tags: selfrootleftsidepackbackgrounddecimalset
1条回答
网友
1楼 · 发布于 2024-05-20 23:33:24

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

相关问题 更多 >