How to Correctly Use `.grid()` with Tkinter - python

I have the following script and the following GUI.
Question(s):
How can I vertically center the 4 boxes on the left side? (There is extra space at the bottom).
I currently have grid with rows and columns which I thought would default vertically center, but that seems to not be the case. Or I did it wrong?
Within mid, I have 2 frames that contain a series of buttons. I tried to place one of the frames above the buttons using the grid(row=) but that doesn't move the frame at all. I expected the behavior of the left view that contains the boxes but it didn't work out. How can I get one frame of boxes above the other?
Note: I'm sure there are simple solutions, I'm just missing it.
Note: I know I could use iteration for most of this, but I was looking to build up my tkinter skills hard-coding and then re-develop with more advanced topics.
Tkinter Script.txt
import tkinter as tk
from tkinter import *
#####################################################################################################
# UI
# Main Window
window = tk.Tk()
window.geometry('700x800')
## 3 Frames
left = tk.Frame(master=window)
mid = tk.Frame(master=window)
right = tk.Frame(master=window)
left.grid(row=0, column=1)
mid.grid(row=0, column=2)
right.grid(row=0, column=3)
## left
### Box1
box1_frame = tk.Frame(master=left)
box1_frame.grid(row=0, column=0)
#### Label
box1_label = tk.Label(master=box1_frame, text="Box1")
box1_label.pack()
#### Text
box1_text = tk.Text(master=box1_frame, width=20, height=10)
box1_text.pack()
### Box2
box2_frame = tk.Frame(master=left)
box2_frame.grid(row=1, column=0)
#### Label
box2_label = tk.Label(master=box2_frame, text="Box2")
box2_label.pack()
#### Text
box2_text = tk.Text(master=box2_frame, width=20, height=10)
box2_text.pack()
### Box3
box3_frame = tk.Frame(master=left)
box3_frame.grid(row=2, column=0)
#### Label
box3_label = tk.Label(master=box3_frame, text="Box3")
box3_label.pack()
#### Text
box3_text = tk.Text(master=box3_frame, width=20, height=10)
box3_text.pack()
### Box4
box4_frame = tk.Frame(master=left)
box4_frame.grid(row=3, column=0)
#### Label
box4_label = tk.Label(master=box4_frame, text="Box4")
box4_label.pack()
#### Text
box4_text = tk.Text(master=box4_frame, width=20, height=10)
box4_text.pack()
## Mid
### Buttons
button_frame = tk.Frame(master=mid)
button_frame.grid(row=0, column=0)
for r in range(3):
for c in range(4):
tk.Button(mid, text="Ironman", borderwidth=1).grid(row=r, column=c, padx=12, pady=4)
some_frame = tk.Frame(master=mid)
some_frame.grid(row=0, column=1)
for r in range(3):
for c in range(4):
tk.Button(mid, text="Taco", borderwidth=1).grid(row=r, column=c, padx=12, pady=4)
#####################################################################################################
# Functions
# Start main loop
window.mainloop()
Edit 1:
I changed it to the correct parent view for the individual buttons and still obtain the SAME output. Nothing has changed.
Mid
Buttons
button_frame = tk.Frame(master=mid)
button_frame.grid(row=0, column=0)
for r in range(3):
for c in range(4):
tk.Button(button_frame, text="Ironman", borderwidth=1).grid(row=r, column=c, padx=12, pady=4)
some_frame = tk.Frame(master=mid)
some_frame.grid(row=0, column=1)
for r in range(3):
for c in range(4):
tk.Button(some_frame, text="Taco", borderwidth=1).grid(row=r, column=c, padx=12, pady=4)
Output

Related

How can I limit pick a question from a list only once in a random order?

I am trying to modify my flashcard python script. It works fine, but what I am triying to do is to modify the random function so that a single question (and its answer) it is not chosen twice. The script should not continue picking questions after the list is completed. I would also need to add a button that let me brwose back to previuos selections.
Any suggestions?
from tkinter import *
from tkinter import ttk
from random import randint
root = Tk()
root.title('Domande')
root.geometry("850x400")
root.resizable(True, True)
words = [
(("Stadio Olimpico"), ("ROMA")),
(("Stadio Artemio Franchi"), ("FIORENTINA")),
(("Stadio Armando Picchi"), ("LIVORNO")),
(("Stadio Luigi Ferraris"), ("SAMPDORIA"))
(("Stadio Arechi"), ("SALERNITANA"))
]
# get a count of our wods list
count = len(words)
def next():
global hinter, hint_count
#Clear screen
answer_label.config(text="")
label.config(text="")
hint_label.config(text="")
#Reset Hint stuff
hinter = ""
hint_count = 0
#Create random selection
global random_domande
random_domande = randint(0, count-1)
#Update label with Domande
domande.config(text=words[random_domande][0])
#Keep track of the hint
hinter = ""
hint_count = 0
def hint():
global hint_count
global hinter
if hint_count < len(words[random_domande][1]):
hinter = hinter + words[random_domande][1][hint_count]
hint_label.config(text=hinter)
hint_count +=1
#Define function to hide the widget
def hide_widget(widget):
widget.pack_forget()
widget.config(text=words[random_domande][1])
#Define a function to show the widget
def show_widget(widget):
widget.pack()
widget.config(text=words[random_domande][1])
#Labels
domande = Label(root, text="", font=("Times", 14))
domande.pack(side=TOP)
#Create an Label Widget
label = Label(root, text="", font=("Times", 14))
label.pack()
#Create Buttons
button_frame = Frame(root)
button_frame.pack()
hint_button = Button(button_frame, text="Hint", command=hint)
hint_button.pack()
button_show = Button(root, text= "Show", command= lambda:show_widget(label))
button_show.pack()
#Create a button Widget
button_hide = Button(root, text= "Hide", command=lambda:hide_widget(label))
button_hide.pack()
next_button = Button(button_frame, text="Next", command=next)
next_button.pack()
#Create Hint Label
hint_label = Label(root, text="")
hint_label.pack()
answer_label = Label(root, text="", font=("Times", 14))
answer_label.pack(side=BOTTOM)
#Run next function when the program starts
next()
root.mainloop()

Tkinter - Scrollbar does not resize when number of labels inside a frame changes

I am trying to create a GUI where left hand side is a Listbox (contained inside a frame f2) that displays employee ID's and right side is another frame second_frame (contained inside canvas and outer frame f3) that shows transaction details of each selected employee in the form of labels.
Each employee can have multiple transactions. So, The number of labels had to be dynamic, i.e. for first selected item in listbox, there could be two labels and for second selected item in listbox, it could be hundred. For every selection, I am calling two functions to destroy old labels and create new labels. While the code works fine, I am having trouble resizing the scrollbar according to the selected listbox entry. I am new to Tkinter, Please advise. Below is my code.
Also note, the test() function when called from outside any function displays the scroll bar, but does not display anything when called from within any function.
# -*- coding: utf-8 -*-
from tkinter import *
'''def test():
for i in range(0,50):
for j in range (0,7):
Label(second_frame, text=f'{i}{j}', width=20).grid(row=i, column=j, pady=5,padx=5)
'''
# --- function ---
def destroy_frame():
#f1.grid_forget()
print("destroying frame")
for label in second_frame.winfo_children():
label.destroy()
def create_frame(val):
print("creating new frame")
for i in range(0,val):
for j in range (5):
Label(second_frame, text=f'{i} {j} ', relief=GROOVE, width=10).grid(row=i, column=j, pady=5,padx=5)
def on_selection(event):
# here you can get selected element
print('previous:', listbox.get('active'))
print(' current:', listbox.get(listbox.curselection()))
# or using `event`
print('(event) previous:', event.widget.get('active'))
print('(event) current:', event.widget.get(event.widget.curselection()))
print (listbox.get(listbox.curselection()))
if (listbox.get(listbox.curselection()) == "Eid 1"):
destroy_frame()
create_frame(100)
elif (listbox.get(listbox.curselection()) == "Eid 2"):
destroy_frame()
create_frame(200)
print('---')
root = Tk()
root.geometry('800x500')
#Create base Frames
f1 = Frame(width=800, height=50, bg="yellow", colormap="new")
f1.grid(row=0, columnspan=2)
f1.grid_propagate(False)
f2 = Frame(width=200, height=425, bg="light blue", colormap="new")
f2.grid(row=1, column=0)
f2.grid_propagate(False)
f3 = Frame(width=600, height=425, bg="light green", colormap="new")
f3.grid(row=1, column=1)
f3.grid_propagate(False)
#Create header Label
l1_f1 = Label(f1, text="Employee Purchase Entries:", bg="yellow")
l1_f1.grid(row=0, column=0)
#Create Listbox
listbox = Listbox(f2, bg="light blue", width=40, height=400)
listbox.grid(row=0, column=0)
#Add Scrollbar to ListBox
list_scrollbar = Scrollbar(f2)
list_scrollbar.grid(row=0, column=1, sticky=NSEW)
#Enter Listbox Data
listbox.insert(1, 'Eid 1')
listbox.insert(2, 'Eid 2')
listbox.bind('<<ListboxSelect>>', on_selection)
#configure the Listbox and Scrollbar
listbox.config(yscrollcommand = list_scrollbar.set)
list_scrollbar.config(command = listbox.yview)
#Create a Canvas
my_canvas = Canvas(f3, width=580, height=425, bg="light green")
#my_canvas.pack(side=LEFT, fill=BOTH, expand=1)
my_canvas.grid(row=0, column=0)
#Add a Scrollbar to the canvas
my_scrollbar = Scrollbar(f3, orient=VERTICAL, command=my_canvas.yview)
my_scrollbar.grid(row=0, column=1, sticky=NSEW)
#configure the canvas
my_canvas.configure(yscrollcommand=my_scrollbar.set)
my_canvas.bind('<Configure>', lambda e : my_canvas.configure(scrollregion = my_canvas.bbox("all")))
#Create another frame inside the canvas
second_frame = Frame(my_canvas)
#Add the new frame to a window in the canvas
my_canvas.create_window((0,0), window=second_frame, anchor="nw")
#test()
root.mainloop()
Your canvas isnt trigger the configure event when you add widgets to your frame. Instead your frame is been triggerd.
So you need to put this line:
second_frame.bind('<Configure>', lambda e : my_canvas.configure(scrollregion = my_canvas.bbox("all")))
after creating second_frame

Get input values from a grid with several Entry widgets on Tkinter

I'm trying to create a sudoku solver. I have my grid with the entries at this point but I don't know how to get the values that the user has put in them.
I have no clue yet as to how I'm going to make the Sudoku solver but I think I'll first have to find a way to store the input in some variable(s) so I can work with them later on.
So my question is, how do I get the values that have been filled into the entries?
This is my code thus far:
from tkinter import *
root = Tk()
root.title('Sudoku Solver')
root.geometry('500x400')
mylabel = Label(root, text='Fill in the numbers and click solve').grid(row=0, column=0, columnspan=10)
# Create the grid
def beg():
global e
cells = {}
for row in range(1, 10):
for column in range(1, 10):
if ((row in (1,2,3,7,8,9) and column in (4,5,6)) or (row in (4,5,6) and column in (1,2,3,7,8,9))):
kleur='black'
else:
kleur='white'
cell = Frame(root, bg='white', highlightbackground=kleur,
highlightcolor=kleur, highlightthickness=2,
width=50, height=50, padx=3, pady=3, background='black')
cell.grid(row=row, column=column)
cells[(row, column)] = cell
e = Entry(cells[row, column], width=4, bg='white', highlightthickness=0, fg='black', relief=SUNKEN)
e.pack()
# Tell the button what to do
def solve():
global e
test = e.get()
print(test)
# Create the buttons and give them a command
clearer = Button(root, text='Clear', command=beg)
solver = Button(root, text='Solve', command=solve)
# Locate the buttons
clearer.grid(row=11, column=3, pady=30)
solver.grid(row=11, column=7, pady=30)
# Run it for the first time
beg()
root.mainloop()
I also tried changing e to e[row, column] but that gave me a syntax error.
Standard rule: if you have many elements then keep them on list or dictionary.
Do the same as with cells
Create dictionary
entries = {}
add to dictionary
entries[(row, column)] = e
and get from dictionary
def solve():
for row in range(1, 10):
for column in range(1, 10):
print(row, column, entries[(row, column)].get() )
# from tkinter import * # PEP8: `import *` is not preferred
import tkinter as tk
# --- functions ---
# Create the grid
def beg():
# remove old widgets before creating new ones
for key, val in cells.items():
print(key, val)
val.destroy()
for row in range(1, 10):
for column in range(1, 10):
if ((row in (1,2,3,7,8,9) and column in (4,5,6)) or (row in (4,5,6) and column in (1,2,3,7,8,9))):
kleur='black'
else:
kleur='white'
cell = tk.Frame(root, bg='white', highlightbackground=kleur,
highlightcolor=kleur, highlightthickness=2,
width=50, height=50, padx=3, pady=3, background='black')
cell.grid(row=row, column=column)
cells[(row, column)] = cell
e = tk.Entry(cell, width=4, bg='white', highlightthickness=0, fg='black', relief='sunken')
e.pack()
entries[(row, column)] = e
# Tell the button what to do
def solve():
for row in range(1, 10):
for column in range(1, 10):
print(row, column, entries[(row, column)].get() )
# --- main ---
entries = {}
cells = {}
root = tk.Tk()
root.title('Sudoku Solver')
root.geometry('500x400')
mylabel = tk.Label(root, text='Fill in the numbers and click solve')
mylabel.grid(row=0, column=0, columnspan=10)
# Create the buttons and give them a command
clearer = tk.Button(root, text='Clear', command=beg)
solver = tk.Button(root, text='Solve', command=solve)
# Locate the buttons
clearer.grid(row=11, column=2, pady=30, columnspan=3) # I added `columnspan=3`
solver.grid(row=11, column=6, pady=30, columnspan=3) # I added `columnspan=3`
# Run it for the first time
beg()
root.mainloop()

Putting two buttons next to each other with Tkinter

I was coding a GUI with the Tkinter library in Python and I've ran into the following problem:
# DECLARATIONS
labelPresentation = Label(window, text="Insert here")
name = StringVar()
nameEntered = Entry(window, width=25, textvariable=name)
labelPresent = Label(window)
research = ttk.Button(window, text="Search", command=searchToPut)
elimination = ttk.Button(window, text="Delete", command=searchToDelete)
# POSITIONS
labelPresentation.grid(column=0, row=0)
nameEntered.grid(column=0, row=1)
labelPresent.grid(column=0, row=3)
research.grid(column=0, row=2)
elimination.grid(column=0, row=2)
I need to put the elimination and research button next to each other but the grid function is causing me problems, because this code put them one in top of the other. I searched for the same problem and they suggested to use the pack() function, but when I try it it says that I can't use both grid and pack function to place buttons and labels.
Thanks in advance for the help!
Try this:
# DECLARATIONS
labelPresentation = Label(window, text="Insert here")
name = StringVar()
nameEntered = Entry(window, width=25, textvariable=name)
labelPresent = Label(window)
research = ttk.Button(window, text="Search", command = searchToPut)
elimination = ttk.Button(window, text="Delete", command =searchToDelete )
# POSITIONS
#labelPresentation.grid( row=0)
labelPresentation.place(x=50)
nameEntered.place(x=0,y=20)
labelPresent.grid( row=0)
research.grid(row=2,column=0)
elimination.grid(row=2, column = 1 , padx=0,pady = 25)
Output:

scroll bar tkinter not scrolling

#declare gui object and classes
app = Tk() #creates instance of Tk()
app.title('Check sort DCA') # sets title of gui
#---------------------------------------
def keepSuggested(): #button press actions
es.JournalOut('test2')
def UseNew():
es.JournalOut('test1')
#------------------------------
frame=Frame(app,width=500,height=500)
frame.grid(row=0,column=0)
canvas=Canvas(frame,bg='#FFFFFF',width=500,height=500,scrollregion=(0,0,500,500))
hbar=Scrollbar(frame,orient=HORIZONTAL)
hbar.pack(side=BOTTOM,fill=X)
hbar.config(command=canvas.xview)
vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(width=500,height=500)
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
canvas.pack(expand=True,fill=BOTH)
spacer1 = Label(canvas, text='|')
spacer2 = Label(canvas, text='|')
spacer3 = Label(canvas, text='|')
spacer4 = Label(canvas, text='|')
spacer5 = Label(canvas, text='|')
Chan_Num = Label(canvas,text='Channel Number')
Chan_Name = Label(canvas, text='Channel Name')
NewChan_Num = Label(canvas, text='New Channel Number')
Set_Title = Label(canvas, text='Set New')
std_Num=Label(canvas, text='Standard Channel Number')
std_Name = Label(canvas, text='Standard Channel Name')
Chan_Num.grid(row=0, column=0)
spacer1.grid(row=0, column=1)
Chan_Name.grid(row=0, column=2)
spacer2.grid(row=0, column=3)
NewChan_Num.grid(row=0, column=4)
spacer3.grid(row=0, column=5)
Set_Title.grid(row=0, column=6)
spacer4.grid(row=0,column=7)
std_Num.grid(row=0,column=8)
spacer5.grid(row=0,column=9)
std_Name.grid(row=0,column=10)
n=0
i = 0 # loops through all channel numbers to get print table value.
while i < nchan: # prints out all present channels with index and channel number and title #populates tables
ch_name = tsin.GetChanTitle(i)
ch_num = tsin.GetChanNumber(i)
ch_name_list = Label(canvas, text=ch_name )
ch_num_list = Label(canvas, text=str(ch_num))
ch_name_list.grid(row=i + 1, column=2)
ch_num_list.grid(row=i + 1, column=0)
UserInput=StringVar()
EntryBox= Entry(canvas, textvariable = UserInput)
EntryBox.grid(row=i+1,column=4 )
i = i + 1
j=0
while j< len(CorrectChannels):
stdList= CorrectChannels[j]
stdListNum = j
std_ch_num= Label(canvas,text=stdListNum+1)
std_ch_name = Label(canvas,text=stdList)
std_ch_name.grid(row=j+1, column=10)
std_ch_num.grid(row=j+1, column=8)
j=j+1
#build gui elements
Buttonnew = Button(canvas, text='Set Channels', bg='blue', fg='white',command=UseNew)
Buttonnew.grid(row=1, column=6)
Buttonkeep = Button(canvas, text='keep channels', bg='blue', fg='white', command=keepSuggested)
Buttonkeep.grid(row=2, column=6)
app.mainloop()
When I run my tkinter code python code I get a scroll bar with no scroll ability, all of my widgets are in canvas and display properly however scroll is needed to scroll down to see them all, this code is producing a scroll bar however it isn't working.
Your scrollbar works fine. However, you've defined the scrollregion exactly the same as the size of the canvas. Therefore there is nothing to scroll to. Try
canvas=Canvas(frame,bg='#FFFFFF',width=500,height=500,scrollregion=(0,0,500,800))
And you will see that you can scroll down 300 pixels.
Full working example code:
app = Tk()
frame=Frame(app,width=500,height=500)
frame.grid(row=0,column=0)
canvas=Canvas(frame,bg='#FFFFFF',width=500,height=500,scrollregion=(0,0,500,800))
vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(yscrollcommand=vbar.set)
canvas.pack()
canvas.create_rectangle((200,300,300,600))
app.mainloop()
From your comment, I get the impression that you are using grid to place widgets on your Canvas. You should not do that, a Canvas is not a Frame in which you can grid widgets. You can create shapes on a Canvas or create a window that contains widgets. If you are trying to make a grid of widgets scrollable, you should place the Frame on the Canvas, not the other way around. See this answer for a great example of making a grid of widgets scrollable.

Categories

Resources