python tkinter 面板切换

点击第一个按钮的时候,显示text1;点击第二个按钮的时候显示text2,以此类推。。。随便点击哪个按钮,相应的显示哪个text?#-*-coding:utf-8-*-_... 点击第一个按钮的时候,显示text1;点击第二个按钮的时候显示text2,以此类推。。。随便点击哪个按钮,相应的显示哪个text?

# -*- coding:utf-8 -*-
__author__ = 'Administrator'

import Tkinter

root = Tkinter.Tk()
root.title("test")

Frame_module = Tkinter.Frame(root)
Frame_content = Tkinter.Frame(root)

def frame1():
#content_text1.pack(fill=Tkinter.BOTH, expand=1, side=Tkinter.LEFT)
#content_text1.place(x=0, y=0)
content_text1.insert(1.1, '1111111')
content_text1.grid(row=1, column=1)
def frame2():
content_text2.insert(1.1, '2222222')
content_text2.grid(row=1, column=1)

#content_text2.place(x=0, y=0)
#content_text2.pack(side=Tkinter.LEFT)
def frame3():
content_text3.insert(1.1, '333333333')
content_text3.grid(row=1, column=1)

#content_text3.place(x=0, y=0)
#content_text3.pack(side=Tkinter.LEFT)
def frame4():
content_text4.insert(1.1, '444444444')
content_text4.grid(row=1, column=1)
#content_text4.place(x=0, y=0)
#content_text4.pack(side=Tkinter.LEFT)

v = Tkinter.IntVar()
v.set(1)
radiobutton1 = Tkinter.Radiobutton(Frame_module, variable=v, value=1, text='111111', indicatoron=0, relief='ridge', command=frame1)
radiobutton2 = Tkinter.Radiobutton(Frame_module, variable=v, value=2, text='222222', indicatoron=0, relief='ridge', command=frame2)
radiobutton3 = Tkinter.Radiobutton(Frame_module, variable=v, value=3, text='333333', indicatoron=0, relief='ridge', command=frame3)
radiobutton4 = Tkinter.Radiobutton(Frame_module, variable=v, value=4, text='444444', indicatoron=0, relief='ridge', command=frame4)
radiobutton1.pack(side=Tkinter.LEFT)
radiobutton2.pack(side=Tkinter.LEFT)
radiobutton3.pack(side=Tkinter.LEFT)
radiobutton4.pack(side=Tkinter.LEFT)

content_text1 = Tkinter.Text(Frame_content)
content_text2 = Tkinter.Text(Frame_content)
content_text3 = Tkinter.Text(Frame_content)
content_text4 = Tkinter.Text(Frame_content)

Frame_module.pack(pady=5)
Frame_content.pack(pady=5, fill=Tkinter.BOTH, expand=1)

root.mainloop()
展开
 我来答
Jakey2333
2016-10-09 · TA获得超过268个赞
知道小有建树答主
回答量:253
采纳率:100%
帮助的人:173万
展开全部
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import Tkinter as tk

g_font = ("Monaco", 12)


class ShowFrame(object):
    '''
    show frame
    '''

    def __init__(self, master=None):
        self.root = master
        self.create_frame()

    def create_frame(self):
        '''
        create main frame
        '''
        self.frm = tk.Frame(self.root)
        self.frm.pack(fill="both", expand=1)

        self.frm_choose = tk.LabelFrame(self.frm) 
        self.frm_choose.pack(fill="both", expand=1, padx=2, side=tk.TOP)

        self.frm_show = tk.LabelFrame(self.frm) 
        self.frm_show.pack(fill="both", expand=1, padx=2, side=tk.BOTTOM)

        self.create_frm_choose()
        self.create_frm_show()

    def create_frm_choose(self):
        '''
        create frame choose
        '''
        self.choose_info_lst = ["Button0", "Button1", "Button2", "Button3"]
        self.choose_btn_lst = list()
        for index, value in enumerate(self.choose_info_lst):
            temp_btn = tk.Button(self.frm_choose,
                                 anchor="w",
                                 text=value,
                                 font=g_font)
            temp_btn.bind('<Button-1>', self.btn_click)
            temp_btn.pack(fill="both", expand=1, padx=2, pady=2, side=tk.LEFT)
            self.choose_btn_lst.append(temp_btn)

    def create_frm_show(self):
        '''
        create frame show
        '''
        self.show_label_0 = tk.Label(self.frm_show, text="Button0", font=g_font)
        self.show_label_0.pack(fill="both", expand=1, padx=2, pady=2)

        self.show_label_1 = tk.Label(self.frm_show, text="Button1", font=g_font)
        self.show_label_1.pack_forget()

        self.show_label_2 = tk.Label(self.frm_show, text="Button2", font=g_font)
        self.show_label_2.pack_forget()

        self.show_label_3 = tk.Label(self.frm_show, text="Button3", font=g_font)
        self.show_label_3.pack_forget()

    def btn_click(self, event=None):
        '''
        choose frm
        '''
        btn_text = event.widget['text']
        if btn_text == "Button0":
            self.show_label_0.pack(fill="both", expand=1, padx=2, pady=2)
            self.show_label_1.pack_forget()
            self.show_label_2.pack_forget()
            self.show_label_3.pack_forget()
        elif btn_text == "Button1":
            self.show_label_0.pack_forget()
            self.show_label_1.pack(fill="both", expand=1, padx=2, pady=2)
            self.show_label_2.pack_forget()
            self.show_label_3.pack_forget()
        elif btn_text == "Button2":
            self.show_label_0.pack_forget()
            self.show_label_1.pack_forget()
            self.show_label_2.pack(fill="both", expand=1, padx=2, pady=2)
            self.show_label_3.pack_forget()
        elif btn_text == "Button3":
            self.show_label_0.pack_forget()
            self.show_label_1.pack_forget()
            self.show_label_2.pack_forget()
            self.show_label_3.pack(fill="both", expand=1, padx=2, pady=2)

if __name__ == "__main__":
    '''
    main loop
    '''
    root = tk.Tk()
    root.columnconfigure(0, weight=1)
    root.rowconfigure(0, weight=1)
    root.geometry()

    app = ShowFrame(root)
    app.frm.pack(fill="both", expand=1)
    root.mainloop()

提供一种我常用的方法,附件为继承tk控件初始化一些颜色的示例。

示例中是对4个Label切换,实际应用中可以切换对应Frame即可。

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式