update label variable in python tkinter - python

from tkinter import *
import tkinter as tk
import random
f = 0
def test_print():
f = random.randint(0,118)
master.update_idletasks()
# creating Tk window
master = Tk()
master.minsize(600,400)
var = IntVar()
var.set(f)
# cretaing a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !",
background = "red", fg = "white", command = test_print)
b1.pack(side = TOP, expand = True, fill = BOTH)
label = tk.Label(pane, textvariable = var)
label.pack(side = BOTTOM, expand = False)
# Execute Tkinter
master.mainloop()
This code runs fine but it doesn't give me a random number when i press the button. I need it to give me a random output so i can get a random element from a list. Does anybody have the answer to this?

Here it is. Now you can see that the label changes:
from tkinter import *
import tkinter as tk
import random
def test_print():
f = random.randint(0, 118)
label.configure(text=str(f))
master.update_idletasks()
# creating Tk window
master = Tk()
master.minsize(600, 400)
# cretaing a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill=BOTH, expand=True)
# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text="Click me !",
background="red", fg="white", command=test_print)
b1.pack(side=TOP, expand=True, fill=BOTH)
label = tk.Label(pane)
label.pack(side=BOTTOM, expand=False)
# Execute Tkinter
master.mainloop()

here is the answer
from tkinter import *
import tkinter as tk
import random
maxnumber = 118
f = random.randint(0,maxnumber)
def test_print():
master.update_idletasks()
label.configure(textvariable = var)
# creating Tk window
master = Tk()
master.minsize(600,400)
var = IntVar()
var.set(f)
# cretaing a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
# button widgets which can also expand and fill
# in the parent widget entirely
# Button 1
b1 = Button(pane, text = "Click me !",
background = "red", fg = "white", command = test_print)
b1.pack(side = TOP, expand = True, fill = BOTH)
label = tk.Label(pane)
label.pack(side = BOTTOM, expand = False)
# Execute Tkinter
master.mainloop()

Related

Making a window UI scroll down and deleting labels from frame

I am making a random generator for my friends and I'm stuck trying to make a scroll down option. So if you generate more the window can show, a scroll down window should be possible. But I can't seem to get any to work. I've tried many online tutorials.
And my second issue with my code is that I can't clear the generated labels from the window. I got it working that it expands the window.
from cProfile import label
from pickle import FRAME
import random
import tkinter as tk
from tkinter import BOTH, DISABLED, LEFT, RIGHT, VERTICAL, Y, Frame, Label, filedialog, Text
import os
from tkinter import ttk
from tkinter.font import NORMAL
from tkinter.messagebox import YES
root = tk.Tk()
root.title('guesser')
#Pelin arvonta ohjelma !
def delete():
for child in root.children.values():
info = child.grid_info()
if info['column'] == 0:
child.grid_forget()
def arvonta():
global label
list1 = []
lista = ["Valorant","Rainbow","Vampire: The masquerade","Playerunknown's battlegrounds","Fortnite","Left 4 Dead 2","Counter strike Global offensive","Realm roayale","Black ops 1 zombies/multiplayer","Black ops 2 zombies/multiplayer","Black ops 3 zombies/multiplayer"]
numero = random.randint(0, 10)
hahmo = (lista[numero])
list1.append(hahmo)
for app in list1:
label = tk.Label(frame, text=app, bg="red",font=('Helvetica',20))
label.pack()
def valorant():
list2 = []
lista2 = ["Brimstone","Viper","Omen","Killjoy","Cypher","Sova","Sage","phoenix","Jett","Reyna","Raze","Raze","Breach","Skye","Yoru","Astra","Kay/o","Chamber","Neon","Fade"]
numero = random.randint(0, 19)
randomValorantagent=(lista2[numero])
list2.append(randomValorantagent)
for app in list2:
label = tk.Label(frame, text=app, bg="red",font=('Helvetica',20))
label.pack()
def quitter():
quit()
canvas = tk.Canvas(root,height=700,width=700,bg="#263D42")
canvas.pack(side=LEFT,fill=BOTH,expand=1)
frame = tk.Frame(root,bg="green")
frame.place(relwidth=0.8,relheight=0.8,relx=0.1,rely=0.1)
frame.pack(fill=BOTH,expand=1)
my_scrollbar = ttk.Scrollbar(frame, orient=VERTICAL, command=canvas.yview)
my_scrollbar.pack(side=RIGHT, fill=Y)
# Configure The Canvas
canvas.configure(yscrollcommand=my_scrollbar.set)
canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion = canvas.bbox("all")))
# Create ANOTHER Frame INSIDE the Canvas
second_frame = Frame(canvas)
# Add that New frame To a Window In The Canvas
canvas.create_window((0,0), window=second_frame, anchor="nw")
#rlls the game
openfile = tk.Button(second_frame,text="Roll a game",padx=10,pady=5,fg="white",bg="#263D42", command=arvonta)
openfile.pack()
#rolls a valorant agent
valorantA = tk.Button(second_frame,text='Roll valorant agent',padx=10,pady=5,fg="white",bg="#263D42",command=valorant)
valorantA.pack()
# stops program
stop = tk.Button(second_frame,text="Quit",padx=10,pady=5,fg="white",bg="#263D42",command=quitter)
stop.pack()
# deletes all info from screen.
deletor = tk.Button(second_frame,text="delete info",padx=10,pady=5,fg="white",bg="#263D42",command=delete)
deletor.pack()
root.mainloop()```
The following does most of what you want. I wrote it some time ago to test Scrollbars because they are wonky IMHO
from tkinter import *
from functools import partial
class ButtonsTest:
def __init__(self):
self.top = Tk()
self.top.title("Click a button to remove")
self.top.geometry("425x200+50+50")
Label(self.top, text=" Click a button to remove it ",
bg="lightyellow", font=('DejaVuSansMono', 12)
).grid(row=0, sticky="nsew")
Button(self.top, text='Exit', bg="orange", width=9,
command=self.top.quit).grid(row=1,column=0,
sticky="nsew")
self.add_scrollbar()
self.button_dic = {}
self.buttons()
self.top.mainloop()
##-------------------------------------------------------------------
def add_scrollbar(self):
self.canv = Canvas(self.top, relief=SUNKEN)
self.canv.config(width=400, height=200)
self.top_frame = Frame(self.canv, height=100)
##---------- scrollregion has to be larger than canvas size
## otherwise it just stays in the visible canvas
self.canv.config(scrollregion=(0,0, 400, 500))
self.canv.config(highlightthickness=0)
ybar = Scrollbar(self.top, width=15, troughcolor="lightblue")
ybar.config(command=self.canv.yview)
## connect the two widgets together
self.canv.config(yscrollcommand=ybar.set)
ybar.grid(row=3, column=2, sticky="ns")
self.canv.grid(row=3, column=0)
self.canv.create_window(1,0, anchor=NW,
window=self.top_frame)
##-------------------------------------------------------------------
def buttons(self):
b_row=1
b_col=0
for but_num in range(1, 51):
## create a button and send the button's number to
## self.cb_handler when the button is pressed
b = Button(self.top_frame, text = str(but_num), width=5,
command=partial(self.cb_handler, but_num))
b.grid(row=b_row, column=b_col)
## dictionary key=button number --> button instance
self.button_dic[but_num] = b
b_col += 1
if b_col > 4:
b_col = 0
b_row += 1
##----------------------------------------------------------------
def cb_handler( self, cb_number ):
print("\ncb_handler", cb_number)
self.button_dic[cb_number].grid_forget()
##===================================================================
BT=ButtonsTest()

How to expand entry box with window in tkinter?

I'm just creating simple window using tkinter which have entry box and search button. What is want is when i maximize window search bar also starch but it is not happening,
Here is my code
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("500x500")
root.title("Wikipedia")
class first:
def labels(self):
search_frame = ttk.LabelFrame(root)
search_frame.pack(side = "top",fill = "both")
search_var = tk.StringVar()
search_bar = tk.Entry(search_frame,width = 40,textvariable = search_var)
search_bar.grid(row = 0,column = 0)
search_button = ttk.Button(search_frame,text = "Search")
search_button.grid(row = 1,column = 0)
boot = first()
boot.labels()
root.mainloop()
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("500x500")
root.title("Wikipedia")
class first:
def labels(self):
search_frame = ttk.LabelFrame(root)
search_frame.pack(side="top", fill="both")
search_var = tk.StringVar()
search_bar = tk.Entry(search_frame, width=20, textvariable=search_var)
search_bar.pack(fill="x")
search_button = ttk.Button(search_frame, text="Search")
search_button.pack()
boot = first()
boot.labels()
root.mainloop()
Not sure this is what you're looking for but try it out.
Here I have used fill='x' in the .pack() geometry manager to fill the row

Tkinter Listbox and Scrollbar not displaying

I am having this issue where the scroll bar is not displaying on the listbox. I do not know what the issue is as.
I believe the issue is originating from the Scrollbar variables as the Listbox appears to be displaying and functioning properly.
The output is displaying the listbox with no scrollbar on the right (as set)
Here is the Listbox with the for loop however, it is displaying the wrong dimensions
Here is the code:
#imports
from tkinter import *
from tkinter import messagebox as ms
from tkinter import ttk
import sqlite3
from PIL import Image,ImageTk
import datetime
global time
time = datetime.datetime.now()
class main:
def __init__(self,master):
self.master = master
def search_user_sql(self):
self.search_user_sqlf = Frame(self.master, height=300, width =200)
scrollbar = Scrollbar(self.search_user_sqlf)
scrollbar.pack(side = RIGHT,fill = BOTH)
myList = Listbox(self.search_user_sqlf, yscrollcommand= scrollbar.set)
myList.pack( side = LEFT, fill = BOTH, expand = 2)
scrollbar.config( command = myList.yview )
self.search_user_sql()
root = Tk()
root.title("Gym Membership System")
main(root)
root.mainloop()
You need to pack the frame to display it. To pack() the frame with the correct size settings, try:
search_user_sqlf = Frame(master, height=300, width=200)
search_user_sqlf.pack(expand=True, fill='both')
search_user_sqlf.pack_propagate(0)
Here is how to attach a scrollbar to list set in a frame in Tkinter:
from tkinter import *
master = Tk()
search_user_sqlf = Frame( master, width=400, height=400)
search_user_sqlf.pack(expand=True, fill='both')
search_user_sqlf.pack_propagate(0)
scrollbar = Scrollbar(search_user_sqlf)
scrollbar.pack(side=RIGHT, fill=Y)
myList = Listbox(search_user_sqlf, yscrollcommand=scrollbar.set)
for line in range(100):
myList.insert(END, "This is line number " + str(line))
myList.pack( side = LEFT, fill = BOTH , expand = 2)
scrollbar.config( command = myList.yview )
mainloop()

How to change the colour of everything in a tkinter GUI at once

I have some code (as shown below) which prompts the user to select which colour to change the GUI to. But my problem is that it only changes the background. I'd like to know if there's a way to change the background of every label and button at once or do I have to change each label/button individually.
import tkinter
window = tkinter.Tk()
colour_frame = tkinter.Frame(window)
options_frame = tkinter.Frame(window)
def colours():
options_frame.pack_forget()
red.pack()
orange.pack()
back_button.pack()
colour_frame.pack()
def back():
options_frame.pack()
colour_frame.pack_forget()
def make_red():
window.configure(background="red")
def make_orange():
window.configure(background="orange")
colour_button = tkinter.Button(options_frame, text="Appearance", command=colours)
red = tkinter.Button(colour_frame, text="RED", command=make_red)
red.configure(bg = "red")
orange = tkinter.Button(colour_frame, text="ORANGE", command=make_orange)
orange.configure(bg = "orange")
back_button = tkinter.Button(colour_frame, text="Back", command=back)
window.mainloop()
You can make a list containing all your widgets you want to change
myWidgets = [button1, label1, ... ] # List of widgets to change colour
for wid in myWidgets:
wid.configure(bg = newColour)
Here's an example code of changing the background colour of multiple labels at once.
import tkinter as tk
# Change all label backgrounds
def change_colour():
c = user.get() #Get the entered text of the Entry widget
for wid in widget_list:
wid.configure(bg = c)
# Create GUI
root = tk.Tk()
tk.Label(root, text='Enter a colour').pack()
user = tk.Entry(root)
user.pack()
label_frame = tk.Frame(root)
label_frame.pack()
btn = tk.Button(root, text='Change Colour', command = change_colour)
btn.pack()
widget_list = [user, btn] # Add defined widgets to list
#Dynamicly create labels for example
for x in range(10):
lbl = tk.Label(label_frame, text='Label '+str(x))
lbl.pack(side = tk.LEFT)
widget_list.append(lbl) #Add widget object to list
root.mainloop()
Or if you have a Frame already containing all the widgets you want to change, then you can use this instead.
parent_widget.winfo_children() will return a list containing all the widgets stored inside the parent widget
def change_colour():
c = user.get()
for wid in label_frame.winfo_children():
wid.configure(bg = c)
Try using ttk for some of your GUI elements. ttk allows you to create styles for widgets and update the style to all widgets at once (at least for those that have the same style). You may need to mix the usage of ttk and tkinter, but it should make things a bit easier in the long run. Here is an example I made:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# Creating a style for the buttons
color_style_button = ttk.Style()
color_style_button.configure("color.TButton", foreground="red")
def change_color(color):
# This function changes the style to all buttons using the "color.Button style"
if color == "red":
color_style_button.configure("color.TButton", foreground="red")
elif color == "blue":
color_style_button.configure("color.TButton", foreground="blue")
elif color == "green":
color_style_button.configure("color.TButton", foreground="green")
frame_a = ttk.Frame(root)
frame_a.pack()
red_button = ttk.Button(frame_a, text="Red", command=lambda: change_color("red"), style="color.TButton")
red_button.pack()
blue_button = ttk.Button(frame_a, text="Blue", command=lambda: change_color("blue"), style="color.TButton")
blue_button.pack()
green_button = ttk.Button(frame_a, text="Blue", command=lambda: change_color("green"), style="color.TButton")
green_button.pack()
root.mainloop()
I recommend checking out this site to learn more about ttk and styles.

Python 3.3 Tkinter LabelFrame resizable

Is it possible to create a resizable LabelFrame?
Or any way?
And is it possible to use ttk.PanedWindow with LabelFrame for this?
it's my code:
fram1 = ttk.LabelFrame(root, text = "text1", height = 100, width = 200)
fram1.config(relief=FLAT)
fram1.pack(side = "right", fill="both", expand = True)
fram2 = ttk.LabelFrame(root, text = "text2", height = 100, width = 200)
fram2.config(relief=FLAT)
fram2.pack(side = "left", fill="both", expand = True)
and i can't resize these labelframes
The panedwindow can hold any single widget in a pane so a labelframe is no problem and allows you to add further widgets and children of the labelframe. An example:
import sys
from tkinter import *
from tkinter.ttk import *
def main():
app = Tk()
pw = PanedWindow(app, orient='vertical')
paneA = LabelFrame(pw, text="Pane A", height=240, width=320)
paneB = LabelFrame(pw, text="Pane B", height=240, width=320)
pw.add(paneA, weight=50)
pw.add(paneB, weight=50)
pw.pack(fill='both', expand=True)
app.mainloop()
if __name__=='__main__':
sys.exit(main())
The weight allows you to set a proportionate scaling for each pane as you change the size of the container. If both panes have the same weight then they grow by the same amount.

Categories