我有一个简单的学校作业要做 - 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()