Wednesday, 24 November 2021

Click on a button and open the content in a frame (direct way in frame and way with import script .py)

I would like to click a button and view its content on the rest of the screen (where the gray color is), but through the frame, not across the canvas.

I would need 2 examples, but apply to my code please:

  • example 1: click on button 1 and color the page (as in the classic use in these frames) with the code written in the same single file
  • example 2: click on the button and import an external py file as a module, then the contents of the external file will open completely inside the gray screen

I would need these two simple examples, because for each frame (therefore for each button) I will have a long enough code and I would like to manage it in an orderly and clean way. So I would like to test both examples, but applied to my code.

I've seen examples on the web and on StackOverflow before, but I couldn't apply myself to my code. Can you please help me use my code?

(my code below)

enter image description here

from tkinter import messagebox
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk, Image

root = tk.Tk()
root.title("xxxx")
root.geometry("1920x1080+0+0")
root.config(bg="#f0f0f0")
root.state("normal")

topbar = tk.Frame(root, background="#e10a0a", height=43)
topbar.pack(fill='x') # use pack() instead of place()

leftbar = tk.Frame(root, width=250, background="white")
leftbar.pack(side='left', fill='y') # use pack() instead of place()
leftbar.pack_propagate(0) # disable size auto-adjustment

def clicked(btn):
    for w in leftbar.winfo_children():
        w['bg'] = 'white' if w is not btn else 'yellow'

button1 = tk.PhotoImage(file="/image.png")
btn1 = tk.Button(leftbar, image=button1, borderwidth=0, highlightthickness=0,
                 bg="white", text="aaaaa", foreground='green', compound='left', anchor='w')
btn1.pack(fill='x') # use pack() instead of place()
btn1['command'] = lambda: clicked(btn1)

button2 = tk.PhotoImage(file="/image.png")
btn2 = tk.Button(leftbar, image=button2, borderwidth=0, highlightthickness=0,
                 bg="white", text="bbbbb", foreground='green', compound='left', anchor='w')
btn2.pack(fill='x') # use pack() instead of place()
btn2['command'] = lambda: clicked(btn2)

root.mainloop()


from Click on a button and open the content in a frame (direct way in frame and way with import script .py)

No comments:

Post a Comment