点击一个按钮并打开框架中的内容(在框架中直接打开,在导入脚本.py中打开)
问题描述
我要单击某个按钮并在屏幕的其余部分(灰色部分)查看其内容,但要通过框架查看,而不是跨画布查看。
我需要2个示例,但请应用到我的代码:
- 示例1:单击按钮1并使用同一单个文件中编写的代码为页面上色(与这些框架中的经典用法相同)
- 示例2:点击该按钮,导入一个外部py文件作为模块,外部文件的内容将在灰屏内完全打开
我需要这两个简单的示例,因为对于每个帧(因此对于每个按钮),我将有足够长的代码,我希望以有序和干净的方式管理它。因此,我想测试这两个示例,但将其应用于我的代码。
我以前在Web和StackOverflow上看到过示例,但我无法将自己应用到我的代码中。您能帮我用一下我的代码吗?
(我的代码如下)
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()
解决方案
您可以使用ttk.Notebook
来实现您想要的功能:
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk
# define classes for each page
class Page1(tk.Frame):
def __init__(self, master, **kw):
super().__init__(master, **kw)
tk.Label(self, text='Hello', font='Arial 64 bold').pack(fill='both', expand=1)
class Page2(tk.Frame):
def __init__(self, master, **kw):
super().__init__(master, **kw)
tk.Label(self, text='Python is awesome', font='Times 24 bold', bg=self['bg']).pack()
self.logo = ImageTk.PhotoImage(file='images/python-logo.png')
tk.Label(self, image=self.logo, bg=self['bg']).pack(fill='both', expand=1)
root = tk.Tk()
root.geometry('640x480')
topbar = tk.Frame(root, bg='#e10a0a', height=43)
topbar.pack(fill='x')
style = ttk.Style()
style.theme_use('default') # select a theme that allows configuration of ttk.Notebook
# put the tabs at the left with white background
style.configure('TNotebook', tabposition='wn', background='white', tabmargins=0)
# configure tab with white background initially, yellow background when selected
style.configure('TNotebook.Tab', background='white', width=10, focuscolor='yellow', borderwidth=0)
style.map('TNotebook.Tab', background=[('selected', 'yellow')])
nb = ttk.Notebook(root)
nb.pack(fill='both', expand=1)
img = ImageTk.PhotoImage(file='images/div2.png')
page1 = Page1(nb)
page2 = Page2(nb, bg='pink', bd=0)
nb.add(page1, text='aaaaa', image=img, compound='left')
nb.add(page2, text='bbbbb', image=img, compound='left')
root.mainloop()
输出:
还可以将每个页面的类定义放到外部文件中,例如
page1.py
中的Page1
和page2.py
中的Page2
。然后将它们导入主脚本:
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk
from page1 import Page1
from page2 import Page2
root = tk.Tk()
root.geometry('640x480')
topbar = tk.Frame(root, bg='#e10a0a', height=43)
topbar.pack(fill='x')
style = ttk.Style()
style.theme_use('default') # select a theme that allows configuration of ttk.Notebook
# put the tabs at the left with white background
style.configure('TNotebook', tabposition='wn', background='white', tabmargins=0)
# configure tab with white background initially, yellow background when selected
style.configure('TNotebook.Tab', background='white', width=10, focuscolor='yellow', borderwidth=0)
style.map('TNotebook.Tab', background=[('selected', 'yellow')])
nb = ttk.Notebook(root)
nb.pack(fill='both', expand=1)
img = ImageTk.PhotoImage(file='images/div2.png')
page1 = Page1(nb)
page2 = Page2(nb, bg='pink', bd=0)
nb.add(page1, text='aaaaa', image=img, compound='left')
nb.add(page2, text='bbbbb', image=img, compound='left')
root.mainloop()
page1.py
import tkinter as tk
class Page1(tk.Frame):
def __init__(self, master, **kw):
super().__init__(master, **kw)
tk.Label(self, text='Hello', font='Arial 64 bold').pack(fill='both', expand=1)
page2.py
import tkinter as tk
from PIL import ImageTk
class Page2(tk.Frame):
def __init__(self, master, **kw):
super().__init__(master, **kw)
tk.Label(self, text='Python is awesome', font='Times 24 bold', bg=self['bg']).pack()
self.logo = ImageTk.PhotoImage(file='images/python-logo.png')
tk.Label(self, image=self.logo, bg=self['bg']).pack(fill='both', expand=1)
相关文章