Hi everyone. Let me start by saying I have managed to learn quite a few things and solve pretty much any python problems using this site. So thanks to everyone who contributes to these forums. Now for my problem. I am having a hard time getting the .grid method to work with a particular radiobutton setup in tkinter.

class App(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize() 
        
    def initialize(self):
        ODDS = [
            ('item1', 1),
            ('item3', 3),
            ('item5', 5),
            ('item7', 7),
            ('item9', 9),
            ('item11', 11),
            ('item13', 13),
            ('item15', 15),
        ]

        for text, mode in ODDS:
            self.itemOdds =  Tkinter.Radiobutton(self, text=text,
                        variable=self.itemVar, value=mode)
            self.itemOdds.grid(column=0)

        EVENS = [
            ('item0', 0),
            ('item2', 2),
            ('item4', 4),
            ('item6', 6),
            ('item8', 8),
            ('item10', 10),
            ('item12', 12),
            ('item14', 14),
        ]
        
        for text, mode in EVENS:
            self.itemEvens = Tkinter.Radiobutton(self, text=text,
                        variable=self.itemVar, value=mode)
            self.itemEvens.grid(column=1)
if __name__ == "__main__":
    app = App(None)
    app.title()
    app.geometry('+100+70')
    app.mainloop()

When I do this rather than getting the two sets of buttons side by side, like this:

1....... 2..........
3....... 4..........

They end up offset like this:
1.......
3.......
*************2.........
*************4.........

I have tried every option I can think of using grid. When I add the normal buttons for selection, to try and force the grid manager to push the radiobutton widget up, the normal buttons are just laid over the top of the radio buttons. Anyone have any ideas how to get grid to behave properly?

Recommended Answers

It possibly has something to do with the unconventional way you are including/calling Tkinter. The standard way is [url=http://effbot.org/tkinterbook/tkinter-hello-tkinter.htm]here[/url]. The following code works on my Slackware Linux system. [CODE]import Tkinter

class App():
def init(self):
self.parent = Tkinter.Tk()
self.initialize()
self.parent.mainloop()

def initialize(self):
    self.itemVar = "abc"
    for r in range(8): …
Jump to Post

All 5 Replies

Re: tkinter radiobutton loop causes grid to not work right 80 80

You should supply both the row and column to the grid http://effbot.org/tkinterbook/grid.htm

Re: tkinter radiobutton loop causes grid to not work right 80 80

I have tried that, and though it does bring the two side by side, it has the alternate effect of writing the text for the radiobuttons over eachother. I should probably state that I have tried every combination of row and column placement, as well as sticky use, that I can think of. Nothing has yet to make these two sets display side by side correctly. Everything worked fine when I input each individual radio by itself. I wanted to change the code to this because it is so much more efficient for future changes.

I have edited my original post so that it contains everything necessary to create the window.

Re: tkinter radiobutton loop causes grid to not work right 80 80

It possibly has something to do with the unconventional way you are including/calling Tkinter. The standard way is here. The following code works on my Slackware Linux system.

import Tkinter

class App():
    def __init__(self):
        self.parent = Tkinter.Tk()
        self.initialize() 
        self.parent.mainloop()
 
    def initialize(self):
        self.itemVar = "abc"
        for r in range(8):
            mode = r*2+1
            self.itemOdds =  Tkinter.Radiobutton(self.parent, text="item"+str(mode),
                        variable=self.itemVar, value=mode)
            self.itemOdds.grid(row=r, column=0)
 
        EVENS = [
            ('item0', 0),
            ('item2', 2),
            ('item4', 4),
            ('item6', 6),
            ('item8', 8),
            ('item10', 10),
            ('item12', 12),
            ('item14', 14),
        ]
 
        r = 0
        for text, mode in EVENS:
            self.itemEvens = Tkinter.Radiobutton(self.parent, text=text,
                        variable=self.itemVar, value=mode)
            self.itemEvens.grid(row =r, column=1)
            r += 1

if __name__ == "__main__":
    app = App()
Re: tkinter radiobutton loop causes grid to not work right 80 80

Thank you so much woooee! :D This was driving me insane trying to figure it out. Just want to make sure I am clear in my understanding of something. The portion of your code where you use the variable "r += 1", is that the part telling the grid manager to expand the widget downward instead of writing text on top of itself? Or does that have more to do with assigning the "r" variable rather than a 0 for the row? Thanks again for the help!

Re: tkinter radiobutton loop causes grid to not work right 80 80

is that the part telling the grid manager to expand the widget downward instead of writing text on top of itself?

Yes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.