Python tkinter hide frame

hiding a frame in tkinter

hello everybody
suppose we have a tk frame in witch there are lots of frame,
and we want to hide one of them(frames)
what should we do , and what will be the result in pack or grid goemetry?
thank you in advanced

Most likely, you’ll need to replace the ‘forgotten’ frame with another
one or other widget. You can immediately do *.pack() and it will
replace the frame (assumming you haven’t already packed something else
there).

lots of thanks to the nice guys of python community,

i do it ,and it works just as i want,
but unfourtunatly a new problem arise.
the problem is :
when i use pack_forget , i just work,
but suppose we want to call it in func or method,
i choose to test it in a func:

def do_unpack(f):
f.pack_forget()
.
b=Button(f3,text=»hello»,command=do_unpack(f3))

it does not work & the interpreter does not claim any error.
so how can i send a pointer (refrence) of an object to a func or method.
should i use any prop. of frame instance(ex:f3)?
in other words i want to know how can i send
arg as refrence in python.(ex: object of general MyClass)?
thank you again

i am a real newbie in python & tkinter,

def do_unpack(f):
f.pack_forget()
.
b=Button(f3,text=»hello»,command=do_unpack(f3))

it does not work & the interpreter does not claim any error.

Читайте также:  Python последовательность инициализации переменной

it works perfectly fine, but it doesn’t do what you want.

is a function call, so you’re calling the function *before* you create
the button, and you’re then passing the return value (None) to the
button widget.

if you want to use the function as a callback, use a lambda:

b=Button(. command=lambda: do_unpack(f3))

def cb():
do_unpack(f3)
b=Button(. command=cb)

Источник

Tkinter: how to hide and show frames!

This post belongs to the serie of posts dedicated to my PyBook app to manage text files for ebooks or other purposes. This time we want to introduce a new feature to the app that hides the menu on the left. I will show you a simpler app from scratch to do that, because it would be too complicated to show you that working with the original code with a lot of existing features. In another post I will show you how I added it to the original PyBook app.

Create the app

I create the window passing it to the app App.

root = tk.Tk() app = App(root) root.mainloop()

The class App

The class app in the __init__ method calls the methods for the frame1 (left) and frame2 (right)

class App: def __init__(self, root): self.root = root self.menu() self.text() self.root.bind("", lambda x: self.hide()) self.hidden = 0

The menu on the left

The frame1 contains a listbox with the files in the folder as items. I colored in black (background) and lime (fonts).

def menu(self): self.frame1 = tk.Frame(self.root) self.frame1.pack(side="left", fill=tk.BOTH, expand=1) self.lb = tk.Listbox(self.frame1) self.lb['bg'] = "black" self.lb['fg'] = "lime" self.lb.pack(side="left", fill=tk.BOTH, expand=1) for file in glob.glob("*"): self.lb.insert(tk.END, file)

The text box on the right

There is nothing in it, because this is a simplified version of the PyBook app (watch the links below).

def text(self): self.frame2 = tk.Frame(self.root) self.frame2.pack(side="left", fill=tk.BOTH, expand=1) self.txt = tk.Text(self.frame2) self.txt['bg'] = 'gold' self.txt.pack(fill=tk.BOTH, expand=1)

Binding the hide/show Frame to a key

We’ve seen this code above yet. It makes the computer call the hide() method when you press ctrl+l

self.root.bind("", lambda x: self.hide())

The toggle method to hide and show the frame 1

This does the job, it uses the self.hidden boolean variable to check if the menu on the left is visible or not. It destrois the frame1 when you hit ctrl+l the first time, then destrois the frame2 also and rebuild both menu and text to show them back again. If we were using widgets, we could easily use pack_forget and pack again to hide and show the widgets. In the original App PyBook we need also to trace the selection in the menu to show back the content in the text in the right, but we will see this in the next post.

def hide(self): if self.hidden == 0: self.frame1.destroy() self.hidden = 1 print("Hidden", self.hidden) else: self.frame2.destroy() self.menu() self.text() self.hidden = 0 print("Hidden", self.hidden)

The live coding at 2x speed about tkinter how to hide frames

The whole code

import tkinter as tk import glob class App: def __init__(self, root): self.root = root self.menu() self.text() self.root.bind("", lambda x: self.hide()) self.hidden = 0 def menu(self): self.frame1 = tk.Frame(self.root) self.frame1.pack(side="left", fill=tk.BOTH, expand=1) self.lb = tk.Listbox(self.frame1) self.lb['bg'] = "black" self.lb['fg'] = "lime" self.lb.pack(side="left", fill=tk.BOTH, expand=1) for file in glob.glob("*"): self.lb.insert(tk.END, file) def text(self): self.frame2 = tk.Frame(self.root) self.frame2.pack(side="left", fill=tk.BOTH, expand=1) self.txt = tk.Text(self.frame2) self.txt['bg'] = 'gold' self.txt.pack(fill=tk.BOTH, expand=1) def hide(self): if self.hidden == 0: self.frame1.destroy() self.hidden = 1 print("Hidden", self.hidden) else: self.frame2.destroy() self.menu() self.text() self.hidden = 0 print("Hidden", self.hidden) root = tk.Tk() app = App(root) root.mainloop()

Источник

Читайте также:  What is java api class

How to gray out (disable) a Tkinter Frame?

A Tkinter frame widget can contain a group of widgets. We can change the state of widgets in the frame by enabling or disabling the state of its underlying frame. To disable all the widgets inside that particular frame, we have to select all the children widgets that are lying inside that frame using winfor_children() and change the state using state=(‘disabled’ or ‘enable’) attribute.

Example

In this example, we will create a button and an entry widget. Initially, the state of entry widget is disabled. But when we click the button, it will enable all the widgets in the frame.

#Import the required Libraries from tkinter import * from tkinter import ttk #Define a Function to enable the frame def enable(children): for child in children: child.configure(state='enable') #Create an instance of tkinter frame win = Tk() #Set the geometry of tkinter frame win.geometry("750x250") #Creates top frame frame1 = LabelFrame(win, width= 400, height= 180, bd=5) frame1.pack() #Create an Entry widget in Frame2 entry1 = ttk.Entry(frame1, width= 40) entry1.insert(INSERT,"Enter Your Name") entry1.pack() entry2= ttk.Entry(frame1, width= 40) entry2.insert(INSERT, "Enter Your Email") entry2.pack() #Creates bottom frame frame2 = LabelFrame(win, width= 150, height=100) frame2.pack() #Create a Button to enable frame button1 = ttk.Button(frame2, text="Enable", command=lambda: enable(frame1.winfo_children())) button1.pack() for child in frame1.winfo_children(): child.configure(state='disable') win.mainloop()

Output

Running the above code will display a window that contains two Label Frames. Each frame contains an entry widget and a button to enable or disable the frame.

When we click the «Enable» button, it will activate Frame1.

Источник

Show and hide frames in tkinter

How to show and hide frames in tkinter with pack_forget.

import tkinter as tk on = 1 def close_frame(evt): global on, frame, lbx if on: frame.pack_forget() on = 0 else: frame.pack_forget() frame1.pack_forget() create_frame() on = 1 root = tk.Tk() def create_frame(): """create frame to be hidden when we press k""" global lbx, lbx1, frame, frame1 frame = tk.Frame(root) frame.pack(side="left") lbx = tk.Listbox(frame, bg="gold") lbx.pack() lbx.insert(0, 1) frame1 = tk.Frame(root) frame1.pack() lbx1 = tk.Listbox(frame1, bg="cyan") lbx1.pack(side="left") lbx1.insert(0, 2) create_frame() root.bind("", close_frame) root.mainloop()

Videos


Games

Articoli in evidenza


Giovanni G. Py

I like to solve problems with Python

Читайте также:  Draw an image with php

Seamless Theme Just Pretty , made by Altervista

Источник

Оцените статью