How can i display a 3x3 grid in Python tkinter? - python

For a program i intend to develop, its essential for me to develop a 3x3 grid which will display words from a set which has been converted to a list. To achieve this I'm aware of a method which will supposedly create a 3x3 grid using a list with linebreaks already within the developed list. However, my program functions at a more diverse level and instead of having a pre-defined text file / list, my set/list is defined by the users input and utalises a file dialog which allows the user to select their own 9 words for the 3x3 grid. For this reason i dont think its possible to apply the line breaks. Is there another way i could display a 3x3 grid which displays each of these 9 words. Would be extremely grateful of any advice given. As for code... if anyone feels that any particular section of code would help them solve this problem, do not hesitate to leave a comment. Thank you very much!

Creating a 3x3 grid is no more difficult than looping over items and creating widgets.
This creates a 3x3 grid of labels, storing references to the widgets in a dictionary:
table = tk.Frame(root)
for row in range(3):
for col in range(3):
label = tk.Label(table, text="")
label.grid(row=row, column=col, sticky="nsew", padx=1, pady=1)
table[(row, col)] = label
You can then modify any of the cells of the table using the configure method of the label widget. For example, this sets the middle cell to "Hello":
table[(1,1)].configure(text="Hello")

oyoy, i see dis,
table = tk.Frame(root)
for row in range(3):
for col in range(3):
label = tk.Label(table, text="")
label.grid(row=row, column=col, sticky="nsew", padx=1, pady=1)
table[(row, col)] = label

Related

How can I make an another block of items under a grid in tkinter? - Python

I want to make a 'maze' game with tkinter, and I wanted to display some things under the grid(game area), and this happend:
Because I wanted to put the text in the last line and the first column of the grid.
frame = tk.Frame(window)
frame.grid(row=line+1, column=0)
label = tk.Label(master=frame, text="Dashing:")
label.pack()
Any ideas, how can I fix this?
Make the frame span all of the columns:
frame.grid(row=line+1, column=0, columnspan=17)

Typing in one entry causes all entries to be written into (Tkinter)

I'm creating a sudoku solver desktop application with a GUI using Tkinter. The issue I'm having is when it comes to inputting the board.
Here is the code I'm using:
N = 9
input = [["0" for i in range(N)] for j in range(N)]
for i in range(N):
for j in range(N):
new_col = int(j/3)
new_row = int(i/3)
if (-1)**(new_col+new_row) == 1:
colour = "lightgrey"
else:
colour = "white"
entry = Entry(root, width=10, bg=colour, textvariable=input[i][j])
entry.grid(row=i, column=j)
set_button = Button(root, text="Set", command=set, padx=20, pady=10).grid(row=10,column=3, columnspan=3)
The set button is used to set the values and then display the actual board.
The issue I'm having is that typing in any one of the boxes causes the same value to be typed into every other box. I don't understand how this is happening. Is my array declaration at fault maybe?
Also, I checked and the array is unchanged even after clicking set.
Consider this line of code:
entry = Entry(root, width=10, bg=colour, textvariable=input[i][j])
Because of the way you've initialized input, the above code is the same as this:
entry = Entry(root, width=10, bg=colour, textvariable="0")
Thus, all widgets have the same value for textvariable so they are all linked together and share the same memory for the value.
Each entry needs a unique textvariable, and the value of that option needs to be a tkinter variable object such as StringVar.
That being said, you rarely need to use textvariable. Since you aren't adding a trace to the variables, they are largely unnecessary. I recommend you remove the textvariable and instead save your entry in your array. You can then call the get method of the entry to get the value.

How to place radiobuttons horizontal in python

AANTAL = [(1,"1"),(2,"2"),(3,"3"),(4,"4"),(5,"5"),(6,"6"),]
v= StringVar()
v.set("1")
for text, mode in AANTAL:
but = Radiobutton(Main,padx=20, pady=10,font=('arial', 20, "bold"), bd=4, text=text, variable=v, value=mode, indicatoron=0)
but.grid()
The above code shows some radiobuttons numbered 1 to 6. However, it displays them vertically instead of horizontally. Does anyone know how I could fix this?
I already tried putting row=0 in the grid command but this only stacks the buttons on top of each other instead of spreading them out over a row.
grid has two options for placing a widget. row and column. You need to specify both.
buttons = []
vars = []
for idx, (text, mode) in enumerate(AANTAL):
vars.append(StringVar(value="1"))
buttons.append(Radiobutton(Main,padx=20, pady=10,font=('arial', 20, "bold"), bd=4, text=text, variable=vars[-1], value=mode, indicatoron=0))
buttons[-1].grid(row=0, column=idx)
Also, when using loops to create widgets, it is much better to store them in a list because you can access them later in your program.

Second row entry width affect the first row style in tkinter?

I'm trying to create the some sample application.
Which first row is One label then input entry box then submit button.
Then second row has the another entry box.
My problem is when I increase width of the entry box in second row it affect the first row style. I don't know what is the problem.
import Tkinter
tk_obj = Tkinter.Tk()
tk_geo = tk_obj.geometry("1200x800")
Tkinter.Label(tk_obj, text='Enter query ').grid(row=1,column=1)
def callback():
print "hi"
E1 = Tkinter.Entry(tk_obj,bd=3,width=120)
E1.grid(row=1, column=2,ipady=3)
b = Tkinter.Button(tk_obj, text="Check", command=callback)
b.grid(row=1,column=3)
E2 = Tkinter.Entry(tk_obj,bd=3,width=100)
E2.grid(row=2,column=1,ipady=100)
tk_obj.mainloop()
The grid method places widgets in the center of the cell they inhabit. When you have two widgets of different sizes sharing a row or column, this means that there will be blank space around the smaller widget. To make the second Entry widget span the first two columns, use columnspan=2 when you grid() it. To left-align it within those two columns, use sticky='W':
E2.grid(row=2,column=1,ipady=100, columnspan=2, sticky='W')
You can then adjust that Entry widget's width attribute until it looks the way you want it to.

tkinter present the values from a list to entry widgets

Using tkinter in the gui I'm building an array made of entry boxes. In some columns of the array I'm entering values which i read them and build a list for every column. There are also columns without values which are readonly in which I want to export results from lists. I can't find how to make the program to show the results.
Keep in mind that i have two defs one for building the widgets and one for the calculations.
In order to build one empty-results column I make this:
self.akt_b =[]
for i in range(12):
akt_b =StringVar()
self.akt_b =Entry(self, textvariable =akt_b, width =10,
state='readonly', readonlybackground ="white")
self.akt_b.grid(row =7+i, column =3)
which makes visually what I want.
Then I calculate a list_b of 12 float numbers which I want to show in self.akt_b
I'm trying:
for i in range(12):
self.akt_b[i+1].delete(0, END)
self.akt_b[(i+1)].set("round(list_b[(i+1)], 2)")
which doesn't work
I've tried also .insert not again, I've tried akt_b nothing again
what can I do?
after Kevin's answer I found out the solution, I was missing the .append argument.
Just in case anyone is interested here is the solution:
self.akt_b, self.entries =[], []
for i in range(12):
akt_b =StringVar()
entries =Entry(self, textvariable =akt_b, width =10,
state='readonly', readonlybackground ="white")
entries.grid(row =7+i, column =3)
self.akt_b.append(akt_b)
so now
for i in range(12):
self.akt_b[i].set(round(list_b[i], 2))
works perfect.
Sometimes brain just seems to stop...

Categories