点击一个按钮并打开框架中的内容(在框架中直接打开,在导入脚本.py中打开)

Click on a button and open the content in a frame (direct way in frame and way with import script .py)(点击一个按钮并打开框架中的内容(在框架中直接打开,在导入脚本.py中打开))
本文介绍了点击一个按钮并打开框架中的内容(在框架中直接打开,在导入脚本.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中的Page1page2.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)

这篇关于点击一个按钮并打开框架中的内容(在框架中直接打开,在导入脚本.py中打开)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)