我正在制作一个可以控制机器人的 GUI。
到目前为止,这是我的代码:
from Tkinter import * #Importing TKinter
class Application(Frame): #Making a frame
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title("Vinny's Myro Controller Version 0.1") #Setting the name of the program/window
self.master.geometry("550x365+300+300") #Window dimensions
self.master.rowconfigure(0, weight=2) #how much space the rows take
self.master.columnconfigure(0, weight=1) #how much space the columns take
''' \/ BOTTOM BUTTONS \/ '''
self.master.button1 = Button(master, text = "Take a Picture") #Bottom button that is supposed to take a pic.
self.master.button1.grid(row=6, column=0, sticky=W+E)
self.master.button2 = Button(master, text = "Honk the Horn") #Bottom button that is supposed to honk the horn.
self.master.button2.grid(row=6, column=1, sticky=W+E)
self.master.button3 = Button(master, text = "Get Sensor Data") #Bottom button that is supposed to get data from the sensors.
self.master.button3.grid(row=6, column=2, sticky=W+E)
''' /\ BOTTOM BUTTONS /\ '''
''' \/ LEFT RED FRAME THAT CONTAINS THE ARROWS THAT WILL MOVE THE ROBOT \/ '''
self.frame1 = Frame(master, bg="red") #Background color
self.frame1.grid(row = 0, column = 0, rowspan = 1, columnspan = 1, sticky = W+E+N+S, padx=10, pady=10)
self.frame1.forward = Button(self.frame1, text = "Forward", width=30, height=3) #Forward Button
self.frame1.forward.place(x=63, y=75) #Button Position
self.frame1.right = Button(self.frame1, text = "Right", width=12, height=3) #Right Button
self.frame1.right.place(x=189, y=131) #Button Position
self.frame1.backward = Button(self.frame1, text = "Backward", width=30, height=3) #Backward Button
self.frame1.backward.place(x=63, y=187) #Button Position
self.frame1.left = Button(self.frame1, text = "Left", width=12, height=3) #Left Button
self.frame1.left.place(x=63, y=131) #Button Position
''' /\ LEFT RED FRAME THAT CONTAINS THE ARROWS THAT WILL MOVE THE ROBOT /\ '''
self.frame2 = Frame(master, bg="green")
self.frame2.grid(row = 0, column = 1, rowspan = 3, columnspan = 3, sticky = W+E+N+S, padx=10, pady=10)
self.frame2.button5 = Button(self.frame2, text = "test")
self.frame2.button5.grid(row=6, column=2)
self.frame2.light = BooleanVar()
self.frame2.chk1 = Checkbutton(self, text = "Lights", variable = self.frame2.light, command = 1+1)
self.frame2.chk1.grid(row = 0, column = 3, padx=10, pady = 10)
root = Tk()
app = Application(master=root)
app.mainloop()
注意这段代码:
self.frame2.light = BooleanVar()
self.frame2.chk1 = Checkbutton(self, text = "Lights", variable = self.frame2.light, command = 1+1)
self.frame2.chk1.grid(row = 0, column = 3, padx=10, pady = 10)
不知何故,我可以在我的框架内放置一个按钮,但不能放置一个复选框。我需要这个复选框才能从机器人的传感器中获取数据。
我试过使用网格和位置。
有人可以帮助我吗?
截屏:
谢谢