简体   繁体   中英

tkinter white space when using columnspan

class QuizInterface:
    def __init__(self, quiz: QuizBrain):
        self.quiz = quiz
        # setting windows
        self.window = tk.Tk()

        self.window.title = 'Quiz'
        self.window.config(bg=THEME_COLOR, padx=20, pady=20)
        # setting the frames
        self.f1 = tk.Frame(self.window)
        self.f2 = tk.Frame(self.window)

        for frame in (self.f1, self.f2):
            frame.grid(row=0, column=0, sticky='news')

        # setting f2
        tk.Label(self.f2, text='number of questions', bg=THEME_COLOR,
                 fg='white', font=('Arial', 15, 'normal')).grid(row=0, column=0, columnspan=3)

        self.radio_state = tk.IntVar()
        self.r10 = tk.Radiobutton(self.f2, bg=THEME_COLOR,
                                  text="10", value=10, variable=self.radio_state, command=self.radio_used)
        self.r25 = tk.Radiobutton(self.f2, bg=THEME_COLOR,
                                  text="25", value=25, variable=self.radio_state, command=self.radio_used)
        self.r50 = tk.Radiobutton(self.f2, bg=THEME_COLOR,
                                  text="50", value=50, variable=self.radio_state, command=self.radio_used)
        self.r10.grid(row=1, column=0, sticky='EW')
        self.r25.grid(row=1, column=1, sticky='EW')
        self.r50.grid(row=1, column=2)


        self.window.mainloop()

This is my code. I've cut out some from it, but this is the part where it's causing the problem.

这是它的图片

as you can see, there is a white empty space between option 25 and option 50. I found out that columnspan of the Label affects this. Why is this happening, and how can this be resolved? Thanks in advance.

edit: I found out that it may be caused by the radiobuttons overlapping with each other. But still don't know the solution

Just tweak your last radio button placement and add a sticky attribute to that too:

self.r50.grid(row=1, column=2, sticky='EW')

So that the third one also fills its column

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM