-1
我正在寫一個程序,它創建了一個紅綠燈,我有程序全部設置並且它能正常工作,但是我對格式有一個小問題。我希望單選按鈕位於「紅綠燈」的底部,而不是頂部。讓我的單選按鈕在tkinter的畫布下顯示?
from tkinter import *
class TrafficLight:
def __init__(self):
window = Tk()
window.title("Traffic Light")
frame = Frame(window)
frame.pack()
self.color = StringVar()
rbRed = Radiobutton(frame, text = "Red", variable = self.color, value = "R", command = self.colorChange)
rbRed.grid(row = 1, column = 1)
rbYellow = Radiobutton(frame, text = "Yellow", variable = self.color, value = "Y", command = self.colorChange)
rbYellow.grid(row = 1, column = 2)
rbGreen = Radiobutton(frame, text = "Green", variable = self.color, value = "G", command = self.colorChange)
rbGreen.grid(row = 1, column = 3)
self.canvas = Canvas(window, width = 80, height = 180, bg = "white")
self.canvas.pack()
self.red = self.canvas.create_oval(20,20,60,60, fill = "white")
self.yellow = self.canvas.create_oval(20,70,60,110, fill = "white")
self.green = self.canvas.create_oval(20,120,60,160, fill = "white")
window.mainloop()
def colorChange(self):
on = self.color.get()
if on == "R":
self.canvas.itemconfig(self.red, fill = "red")
self.canvas.itemconfig(self.yellow, fill = "white")
self.canvas.itemconfig(self.green, fill = "white")
elif on == "Y":
self.canvas.itemconfig(self.red, fill = "white")
self.canvas.itemconfig(self.yellow, fill = "yellow")
self.canvas.itemconfig(self.green, fill = "white")
elif on == "G":
self.canvas.itemconfig(self.red, fill = "white")
self.canvas.itemconfig(self.yellow, fill = "white")
self.canvas.itemconfig(self.green, fill = "green")
它看起來像這樣:
我會離開它了,但我想通了自己。我只是在self.canvas.pack()之後放置frame和frame.pack()。只需要進行一些重新排列 – DPlagueis
通常,你可以爲你的''.pack()''調用提供一些參數(''.pack(side ='bottom')''或許)來控制它們相對於其他的打包小部件。 – jasonharper