我一直试图让我的单选按钮工作,我已经查看了其他问题,但无法解决如何修复我的代码。它会打印一个空白区域。
Entry4=StringVar()
def CreateNewJob():
global Entry4
window=Tk()
window.title('Creating new job')
Male= Radiobutton(window, text ='Male' , variable= Entry4 , value='Male')
Female= Radiobutton(window, text ='Female', variable= Entry4 , value='Female')
Mixed= Radiobutton(window, text ='Mixed',variable= Entry4 , value='Mixed')
Male.select()
Male.grid(row=4, column=1)
Female.grid(row=4, column=2)
Mixed.grid(row=4, column=3)
def SubmitNewJob():
print('Selection: ' + Entry4.get())
SubmitButton=Button (window,text= 'Submit', command=SubmitNewJob)
SubmitButton.grid(row=15, column=1)
我已经尝试了所有你提供的所有答案,但我仍然打印出一个空白区域。在这里,我复制了所有代码,因为问题可能出在其余的代码中。
from tkinter import *
window=Tk()
window.geometry("400x400")
#creating the frame and buttons on menu
f=Frame()
button2= Button(f, text= 'Create new job')
button3= Button (f,text = 'Close')
#layout of buttons on menu
button2.grid(row=1, column=0, padx=10, pady=10)
button3.grid(row=2, column=0, padx=10, pady=10)
label = Label( window , text = 'Please choose an option' )
label.pack()
f.pack()
Entry1=None
Entry2=None
Entry3=None
Entry5=None
Entry6=None
Entry7=None
Entry8=None
Entry9=None
Entry10=None
Entry11=None
Entry12=None
Entry13=None
Entry14=None
#allows user to create new job
def CreateNewJob():
window1=Tk()
window1.title('Creating new job')
global Entry2
global Entry3
global Entry4
global Entry5
global Entry6
global Entry7
global Entry8
global Entry9
global Entry10
global Entry11
global Entry12
global Entry13
global Entry14
Entry4=StringVar()
def SubmitNewJob():
print('Selection: ' + Entry4.get())
Label(window1, text = 'Please enter information').grid(row=1)
Label(window1, text = 'Input2').grid(row=2)
Label(window1, text = 'Input3').grid(row=3)
Label(window1, text = 'Input4').grid(row=4)
Label(window1, text = 'Input5').grid(row=5)
Label(window1, text = 'Input6').grid(row=6)
Label(window1, text = 'Input7').grid(row=7)
Label(window1, text = 'Input8').grid(row=8)
Label(window1, text = 'Input9').grid(row=9)
Label(window1, text = 'Input10').grid(row=10)
Label(window1, text = 'Input11').grid(row=11)
Label(window1, text = 'Input12').grid(row=12)
Label(window1, text = 'Input13').grid(row=13)
Label(window1, text = 'Input14').grid(row=14)
Male= Radiobutton(window1, text ='Male' , variable= Entry4 , value='Male')
Female= Radiobutton(window1, text ='Female', variable= Entry4 , value='Female')
Mixed= Radiobutton(window1, text ='Mixed',variable= Entry4 , value='Mixed')
Male.select()
Male.grid(row=4, column=1)
Female.grid(row=4, column=2)
Mixed.grid(row=4, column=3)
Entry2=Entry(window1)
Entry2.grid(row=2, column=1)
Entry3=Entry(window1)
Entry3.grid(row=3, column=1)
Entry5=Entry(window1)
Entry5.grid(row=5, column=1)
Entry6=Entry(window1)
Entry6.grid(row=6, column=1)
Entry7=Entry(window1)
Entry7.grid(row=7, column=1)
Entry8=Entry(window1)
Entry8.grid(row=8, column=1)
Entry9=Entry(window1)
Entry9.grid(row=9, column=1)
Entry10=Entry(window1)
Entry10.grid(row=10, column=1)
Entry11=Entry(window1)
Entry11.grid(row=11, column=1)
Entry12=Entry(window1)
Entry12.grid(row=12, column=1)
Entry13=Entry(window1)
Entry13.grid(row=13, column=1)
Entry14=Entry(window1)
Entry14.grid(row=14, column=1)
#closes submit new job menu
def CloseNewJob():
window1.destroy()
#button to submit new job
SubmitButton=Button (window1,text= 'Submit', command=SubmitNewJob)
SubmitButton.grid(row=15, column=1)
CloseButton=Button (window1,text= 'Close', command=CloseNewJob)
CloseButton.grid(row=15, column=2)
#closes main menu
def Close():
global window
window.destroy()
...
button2.configure(command=CreateNewJob)
button3.configure(command=Close)
window.mainloop()
答案 0 :(得分:0)
您不能将常规变量与Tkinter小部件一起使用。您必须使用其中一个Tkinter变量,例如StringVar
,IntVar
,BooleanVar
和DoubleVar
。有关详细信息,请参阅http://effbot.org/tkinterbook/variable.htm。
注意:您需要在创建变量之前创建根窗口。
更新:
几天后你发布了新代码。在此代码中,您将创建多个Tk
实例。这将导致您的代码失败。发生的事情是StringVar与第一个实例相关联,但是radiobutton与第二个实例相关联,因此它获得了一个单独的StringVar。
如果您需要创建多个窗口,请创建Toplevel
。
答案 1 :(得分:0)
我在这里做了一些改变&那里。第一件事(你已经做过)是使用StringVar()
。
之后你可能会得到:
Exception AttributeError:“StringVar实例没有属性'_tk'”....
您需要做的是先调用Tk()
,然后再调用StringVar()
。其他一切都很好。
from Tkinter import Radiobutton,Button,Tk,StringVar
window=Tk()
window.title('Creating new job')
Entry4=StringVar()
def CreateNewJob():
global Entry4
def SubmitNewJob():
print('Selection: ' + Entry4.get())
Male= Radiobutton(window, text ='Male' , variable= Entry4 , value='Male')
Female= Radiobutton(window, text ='Female', variable= Entry4 , value='Female')
Mixed= Radiobutton(window, text ='Mixed',variable= Entry4 , value='Mixed')
SubmitButton=Button (window,text= 'Submit', command=SubmitNewJob)
SubmitButton.grid(row=15, column=1)
Male.select()
Male.grid(row=4, column=1)
Female.grid(row=4, column=2)
Mixed.grid(row=4, column=3)
window.mainloop()
CreateNewJob()
在您进行新的更改后:如果要创建子窗口,则应使用Toplevel。因此,在您的代码中,只需执行以下操作:
window1=Toplevel()
代替Tk()
答案 2 :(得分:0)
Radiobuttons使用IntVars
def SubmitNewJob():
print('Selection: %d' % (Entry4.get()))
window=Tk()
window.title('Creating new job')
Entry4=IntVar()
Male= Radiobutton(window, text ='Male' , variable= Entry4 , value=1)
Female= Radiobutton(window, text ='Female', variable= Entry4 , value=2)
Mixed= Radiobutton(window, text ='Mixed',variable= Entry4 , value=3)
Male.select()
Male.grid(row=4, column=1)
Female.grid(row=4, column=2)
Mixed.grid(row=4, column=3)....
SubmitButton=Button (window,text= 'Submit', command=SubmitNewJob)
SubmitButton.grid(row=15, column=1)
window.mainloop()