嗨,我製作了一個運行各種其他程序的程序。我導入模塊並且對於沒有涉及單選按鈕的程序工作正常。帶有單選按鈕的程序在單獨運行時可以正常工作。當它們通過我的菜單程序時,單選按鈕不會給出IntVar值。任何人有任何想法如何解決這個問題?這些模塊是我的程序,但他們都使用Tkinter。下面是菜單代碼:用單選按鈕導入模塊時遇到問題 - python
from Tkinter import *
def cmd():
if var.get() == 1:
import animal_age_calculator
animal_age_calculator.createDisplay()
if var.get() == 2:
import temperatureCalculator
temperatureCalculator.createDisplay()
if var.get() == 3:
import calculator
calculator.createDisplay()
if var.get() == 4:
import currencyConverter
currencyConverter.createDisplay()
def createDisplay():
global var
root = Tk()
root.title('Calculator')
title = Label(root, text='Please Select which Calculator you would like to use', font=50)
title.grid(row=0, column=0)
calcType = Frame(root)
calcType.grid(row=1, column=0)
var = IntVar()
animal = Radiobutton(calcType, text='Animal Age Calculator', variable=var, value=1)
animal.pack()
tempConverter = Radiobutton(calcType, text='Temperature Calculator', variable=var, value=2)
tempConverter.pack()
calc = Radiobutton(calcType, text='Calculator', variable=var, value=3)
calc.pack()
currency = Radiobutton(calcType, text='Currency Converter', variable=var, value=4)
currency.pack()
select = Button(root, text='Go', command=cmd)
select.grid(row=2, column=0)
root.mainloop()
if __name__ == '__main__':
createDisplay()
下面是單選按鈕節目之一代碼:
from Tkinter import *
def convert(temperature):
global var4
a = var4.get()
print(str(a))
if a == 1:
print 'test2'
display.delete(1.0, END)
finalTemp = int(temperature)*1.8+32
finalTemp = round(finalTemp, 1)
display.insert(INSERT, finalTemp)
if a == 2:
display.delete(1.0,END)
finalTemp = int(temperature)-32/1.8
finalTemp = round(finalTemp, 1)
display.insert(INSERT, finalTemp)
def createDisplay():
global display, var4
root = Tk()
root.title('Temperature Converter')
root.resizable(width=False, height=False)
title = Label(root, text="Welcome to the temperature converter", font=36)
title.grid(row=0, column=1, columnspan=3)
selTemp = LabelFrame(root, text="Please select your temperature")
selTemp.grid(row=1, column=2)
temp = Scale(selTemp, orient=HORIZONTAL, sliderlength=20, from_=0, to=250, length=250, command=convert)
temp.pack()
temperature = temp.get()
displayFrame = LabelFrame(root, text="Your converted temperature is")
displayFrame.grid(row=1, column=3)
display = Text(displayFrame, width=6, height=1)
display.pack()
var4 = IntVar()
tempType = LabelFrame(root, text='Please select your temperature you would like to convert')
tempType.grid(row=1, column=1)
celcius = Radiobutton(tempType, text="Celcius", variable=var4, value=1)
celcius.pack()
fahrenheit = Radiobutton(tempType, text="Fahrenheit", variable=var4, value=2)
fahrenheit.pack()
root.mainloop()
if __name__ == '__main__':
createDisplay()
你能舉一些例子代碼嗎?也請提供您正在使用的模塊的詳細信息。 – Ffisegydd
模塊是我自己的Python代碼,但它們都使用Tkinter。 –
這是一個好的開始,但您可能需要給出一些示例代碼供人們在嘗試解決您的問題時與之合作:)如果您遇到錯誤,那麼如果您還可以提供可能有用的Traceback。 – Ffisegydd