#๐Ÿ”’ tkinter help

53 messages ยท Page 1 of 1 (latest)

delicate path
#

my goal with this code is make something on the screen at the touch of a button (in this case make a text appear) and be able to clear it once i touch another button (using frame clear)

import tkinter as tk

def clear_all_inside_frame():
    for widget in frame1.winfo_children():
        widget.destroy()  

def testo():
    text="testo"
    text_output = tk.Label(frame1, text=text, fg="red", font=("Helvetica", 16))
    text_output.grid(row=0, column=1)

root = tk.Tk()
root.title("clear frame")
root.geometry("400x200")
# Frame 
frame1 = tk.Frame(root, width=300, height=100)
frame1.pack()

btn1 = tk.Button(root, text="This is a button inside frame", command=testo())
btn1.pack()

clear_button = tk.Button(root,text="Clear all",command=clear_all_inside_frame)
clear_button.pack(pady=25)
root.mainloop()```
this is my code, once i run it the text alredy appears (without needing to touch the button). once i touch the clear button it clears the text but it dosent re-appear even if i touch the first button.
silk vaporBOT
#

@delicate path

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

delicate path
warm stream
delicate path
#

Sorry if I ask but I am very new to tkinter and I am trying to understand it for a project I need to do for school

warm stream
#

Because you are literally destroying the object when you use the destroy method

#

Instead of destroy, use pack_forget

#

It keeps the widget intact but removes it

#

It's usually a good idea to pit everything you want to show/hide into a single frame and just pack/pack_forget that. That way you don't have to do every child

delicate path
#

Also, is it right to think a frame like a div?

#

Or are they 2 opposite things

warm stream
#

it's a container that can hold other widgets

#

you can use the same space for 2 frames, as long as you unpack one before you pack the other

delicate path
warm stream
#

When we create a widget, we have 3 ways we can then display it

#

pack
grid
place

#

pack simply adds it below the previous widget. It has a few extra options for adjusting how it aligns to things, but in general, it adds it next to the previously added widget

#

grid lets you specify rows and columns for a bit more control of the overall layout

#

place is for absolute pixel placement and should generally be avoided except very specific situations

#

you cannot mix pack and grid within the same frame. Once something has been packed, you cannot then grid something to that same frame

#

but, you can nest frames, so you can have a frame with gridded widgets, and that frame can be packed somewhere else

warm stream
#

This is a really important part of UI design

delicate path
#

So let's say that I have 2 packed widgets. The second one is going to be directly under the first. Like it does with divs

warm stream
#

yeah

delicate path
#

And I can modify this by putting them side by side controlling the placement

#

Using grid

warm stream
#

you can pack something side by side too

#

if you just want a row or column of widgets, use pack

#

if you want something a bit more versatile, use a grid

#

something like this is grid

#

column 0 is the radiobutton, column 1 is the label, column 2 is the entry

#

or something like a calculator would be great for grid

delicate path
#

Oh ok, I am starting to understand

#

I will try

warm stream
delicate path
warm stream
#

Is this for school or are you just learning on your own?

delicate path
#

but i have 0 text book material

warm stream
#

.rp tkinter

placid sigilBOT
#

Here are the top 5 results:

Python GUI Programming: Your Tkinter Tutorial
Build a Tic-Tac-Toe Game With Python and Tkinter
Bypassing the GIL for Parallel Processing in Python
Python sleep(): How to Add Time Delays to Your Code
Arduino With Python: How to Get Started
warm stream
#

I recommend this first article as a good intro

delicate path
#

literally the teacher said "connect access with python and create a database viewer using ui"

#

after my quesion "what library do we have to use" the answer was "idk, use what you prefer"

delicate path
silk vaporBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.