Python 让我的单选按钮显示在tkinter的画布下?

Python 让我的单选按钮显示在tkinter的画布下?,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我正在写一个创建红绿灯的程序,我把程序都设置好了,它工作正常,但是我在格式上有一个小问题。我希望单选按钮位于“红绿灯”的底部,而不是顶部 from tkinter import * class TrafficLight: def __init__(self): window = Tk() window.title("Traffic Light") frame = Frame(window) frame.pack()

我正在写一个创建红绿灯的程序,我把程序都设置好了,它工作正常,但是我在格式上有一个小问题。我希望单选按钮位于“红绿灯”的底部,而不是顶部

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")
看起来是这样的:

当您使用
pack()
这是放置在初始部分时,您必须更改顺序

from tkinter import *

class TrafficLight:

    def __init__(self):

        window = Tk()
        window.title("Traffic Light")

        self.canvas = Canvas(window, width = 80, height = 180, bg = "white")
        self.canvas.pack()

        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.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")

TrafficLight()

我不说了,但我自己解决了。我只是将frame和frame.pack()放在self.canvas.pack()之后。只需要一些重新排列通常,您会为
.pack()
调用(
.pack(side='bottom')
)提供一些参数,以控制它们相对于其他小部件的排列位置。