python - How to arrange numbers in Label into grid in Tkinter? -
i create sudoku solver , want show candidates each cell. sudoku grid list of label widgets , have problem arranging these candidates grid. goal this (without colors). have them in grid, number "0" still visible. , i´m asking how hide these zeros. tried substitute "0" space (" "), brackets displayed instead.
my code:
from tkinter import * root = tk() text_good = [1,2,3,4,5,6,7,8,9] text_wrong1 = [1,2,3,4,0,0,0,8,9] text_wrong2 = [1,2,3,4," "," "," ",8,9] l1 = label(root,bg="red",text=text_good,wraplength=30) l1.place(x=0,y=0,width=50,height=50) l2 = label(root,bg="red",text=text_wrong1,wraplength=30) l2.place(x=50,y=0,width=50,height=50) l3 = label(root,bg="red",text=text_wrong2,wraplength=30) l3.place(x=100,y=0,width=50,height=50) root.mainloop()
i hope described problem well. appreciate answer.
thank you
use text_wrong2 = ' '.join(map(str,[1,2,3,4," "," "," ",8,9]))
instead of passing directly list text option. remember default font not monospaced, numbers won't aligned in other 2 examples.
apart that, recommend use grid
instead of place
, , represent each number using label
, instead of using 1 each cell , relying on new lines added because of width. thus, don't have worry handling offsets each label.
from tkinter import * root = tk() text_good = [1,2,3,4,5,6,7,8,9] text_wrong1 = [1,2,3,4,0,0,0,8,9] text_wrong2 = [1,2,3,4," "," "," ",8,9] def create_box(text_list, **grid_options): frame = frame(root, bg="red") i, text in enumerate(text_list): label(frame, text=text, bg="red").grid(row=i//3, column=i%3) frame.grid(**grid_options) create_box(text_good, row=0, column=0, padx=10) create_box(text_wrong1, row=0, column=1, padx=10) create_box(text_wrong2, row=0, column=2, padx=10) root.mainloop()
Comments
Post a Comment