简体   繁体   English

gui 使用 tkinter python

[英]gui using tkinter python

main.py主文件

import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
from PIL import ImageTk, Image
from tkinter import *


import main_support


def popup1(event, *args, **kwargs):
        Popupmenu1 = tk.Menu(main_support.root, tearoff=0)
        Popupmenu1.configure(activebackground="#ececec")
        Popupmenu1.configure(activeborderwidth="1")
        Popupmenu1.configure(activeforeground="#000000")
        Popupmenu1.configure(background="#d9d9d9")
        Popupmenu1.configure(borderwidth="1")
        Popupmenu1.configure(disabledforeground="#a3a3a3")
        Popupmenu1.configure(font="-family {Segoe UI} -size 9")
        Popupmenu1.configure(foreground="#000000")
        Popupmenu1.post(event.x_root, event.y_root)


class Header:
    def __init__(self, top=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#ececec' # Closest X11 color: 'gray92'
        self.style = ttk.Style()
        self.methodSelectionValue = []
        if sys.platform == "win32":
            self.style.theme_use('winnative')
        self.style.configure('.',background=_bgcolor)
        self.style.configure('.',foreground=_fgcolor)
        self.style.configure('.',font="TkDefaultFont")
        self.style.map('.',background=[('selected', _compcolor), ('active',_ana2color)])

        top.geometry("600x450+664+264")
        top.minsize(120, 1)
        top.maxsize(1924, 1061)
        top.resizable(1,  1)
        top.title("Toplevel 0")
        top.configure(background="#d9d9d9")
        top.configure(highlightbackground="#f0f0f0f0f0f0")
        top.configure(highlightcolor="black")

        self.top = top
        self.combobox = tk.StringVar()

        self.Label1 = tk.Label(self.top)
        self.Label1.place(relx=-0.033, rely=0.0, height=41, width=634)
        self.Label1.configure(activebackground="#f9f9f9")
        self.Label1.configure(activeforeground="black")
        self.Label1.configure(background="#b90746")
        self.Label1.configure(compound='center')
        self.Label1.configure(disabledforeground="#a3a3a3")
        self.Label1.configure(font="-family {Calisto MT} -size 16 -weight bold -slant italic")
        self.Label1.configure(foreground="#ffffff")
        self.Label1.configure(highlightbackground="#d9d9d9")
        self.Label1.configure(highlightcolor="black")
        self.Label1.configure(text='''Calculator''')

        self.TFrame1 = ttk.Frame(self.top)
        self.TFrame1.place(relx=0.02, rely=0.12, relheight=0.856, relwidth=0.958)

        self.TFrame1.configure(relief='groove')
        self.TFrame1.configure(borderwidth="2")
        self.TFrame1.configure(relief="groove")

        self.Labelframe1 = tk.LabelFrame(self.TFrame1)
        self.Labelframe1.place(relx=0.47, rely=0.026, relheight=0.948, relwidth=0.504)
        self.Labelframe1.configure(relief='groove')
        self.Labelframe1.configure(foreground="black")
        self.Labelframe1.configure(text='''Main Window''')
        self.Labelframe1.configure(background="#d9d9d9")
        self.Labelframe1.configure(highlightbackground="#d9d9d9")
        self.Labelframe1.configure(highlightcolor="black")

        self.Label2 = tk.Label(self.Labelframe1)
        self.Label2.place(relx=0.038, rely=0.11, height=31, width=90, bordermode='ignore')
        self.Label2.configure(activebackground="#f9f9f9")
        self.Label2.configure(activeforeground="black")
        self.Label2.configure(anchor='w')
        self.Label2.configure(background="#d9d9d9")
        self.Label2.configure(compound='left')
        self.Label2.configure(disabledforeground="#a3a3a3")
        self.Label2.configure(foreground="#000000")
        self.Label2.configure(highlightbackground="#d9d9d9")
        self.Label2.configure(highlightcolor="black")
        self.Label2.configure(text='''Class Selection''')

        self.TCombobox1 = ttk.Combobox(self.Labelframe1, values=['Basic', 'Advance'])
        self.TCombobox1.bind("<<ComboboxSelected>>", self.setMethodValue)
        self.TCombobox1.place(relx=0.421, rely=0.121, relheight=0.058, relwidth=0.531, bordermode='ignore')
        self.TCombobox1.configure(textvariable=self.combobox)
        self.TCombobox1.configure(takefocus="")

        self.Label3 = tk.Label(self.Labelframe1)
        self.Label3.place(relx=0.038, rely=0.274, height=21, width=111, bordermode='ignore')
        self.Label3.configure(activebackground="#f9f9f9")
        self.Label3.configure(activeforeground="black")
        self.Label3.configure(anchor='w')
        self.Label3.configure(background="#d9d9d9")
        self.Label3.configure(compound='left')
        self.Label3.configure(disabledforeground="#a3a3a3")
        self.Label3.configure(foreground="#000000")
        self.Label3.configure(highlightbackground="#d9d9d9")
        self.Label3.configure(highlightcolor="black")
        self.Label3.configure(text='''Method Selection''')

        self.TCombobox2 = ttk.Combobox(self.Labelframe1, values=self.methodSelectionValue)
        self.TCombobox2.place(relx=0.414, rely=0.274, relheight=0.058, relwidth=0.528, bordermode='ignore')
        self.TCombobox2.configure(textvariable=self.combobox)
        self.TCombobox2.configure(takefocus="")

        self.TEntry1 = ttk.Entry(self.Labelframe1)
        self.TEntry1.place(relx=0.138, rely=0.493, relheight=0.112, relwidth=0.262, bordermode='ignore')
        self.TEntry1.configure(takefocus="")
        self.TEntry1.configure(cursor="ibeam")

        self.Label4 = tk.Label(self.Labelframe1)
        self.Label4.place(relx=0.207, rely=0.411, height=21, width=74, bordermode='ignore')
        self.Label4.configure(activebackground="#f9f9f9")
        self.Label4.configure(activeforeground="black")
        self.Label4.configure(anchor='w')
        self.Label4.configure(background="#d9d9d9")
        self.Label4.configure(compound='left')
        self.Label4.configure(disabledforeground="#a3a3a3")
        self.Label4.configure(foreground="#000000")
        self.Label4.configure(highlightbackground="#d9d9d9")
        self.Label4.configure(highlightcolor="black")
        self.Label4.configure(text='''Input 1''')

        self.TEntry2 = ttk.Entry(self.Labelframe1)
        self.TEntry2.place(relx=0.621, rely=0.493, relheight=0.112, relwidth=0.262, bordermode='ignore')
        self.TEntry2.configure(takefocus="")
        self.TEntry2.configure(cursor="ibeam")

        self.Label5 = tk.Label(self.Labelframe1)
        self.Label5.place(relx=0.655, rely=0.411, height=21, width=44, bordermode='ignore')
        self.Label5.configure(activebackground="#f9f9f9")
        self.Label5.configure(activeforeground="black")
        self.Label5.configure(anchor='w')
        self.Label5.configure(background="#d9d9d9")
        self.Label5.configure(compound='left')
        self.Label5.configure(disabledforeground="#a3a3a3")
        self.Label5.configure(foreground="#000000")
        self.Label5.configure(highlightbackground="#d9d9d9")
        self.Label5.configure(highlightcolor="black")
        self.Label5.configure(text='''Input 2''')

        self.Button1 = tk.Button(self.Labelframe1)
        self.Button1.place(relx=0.397, rely=0.685, height=34, width=57, bordermode='ignore')
        self.Button1.configure(activebackground="#ececec")
        self.Button1.configure(activeforeground="#000000")
        self.Button1.configure(background="#d9d9d9")
        self.Button1.configure(compound='left')
        self.Button1.configure(disabledforeground="#a3a3a3")
        self.Button1.configure(foreground="#000000")
        self.Button1.configure(highlightbackground="#d9d9d9")
        self.Button1.configure(highlightcolor="black")
        self.Button1.configure(pady="0")
        self.Button1.configure(text='''Run''')

        self.TFrame2 = ttk.Frame(self.TFrame1)
        self.TFrame2.place(relx=0.017, rely=0.026, relheight=0.948, relwidth=0.426)
        self.TFrame2.configure(relief='groove')
        self.TFrame2.configure(borderwidth="2")
        self.TFrame2.configure(relief="groove")

    def setMethodValue(self, event):
        selectedValue = self.TCombobox1.get()
        if selectedValue == "Basic":
            self.TCombobox2["values"] = ["Add", "Sub"]
        elif selectedValue == "Advance":
            self.TCombobox2["values"] = ["Multiply", "Division", "Modulus"]
        self.TCombobox2.current()


def start_up():
    main_support.mains()


if __name__ == '__main__':
    main_support.mains()

main_support.py main_support.py

import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *

import main


def mains(*args):
    '''Main entry point for the application.'''
    global root
    root = tk.Tk()
    root.protocol( 'WM_DELETE_WINDOW' , root.destroy)
    # Creates a toplevel widget.
    global _top1, _w1
    _top1 = root
    _w1 = main.Header(_top1)
    root.mainloop()


if __name__ == '__main__':
    main.start_up()

Created two dropdowns one is for class selection and other is for method selection of that particular class.创建了两个下拉列表,一个用于 class 选择,另一个用于选择特定 class 的方法。 class name: basic, advance, method selection: basic: add,sub, advance:multiply, division and modulus. class 名称:基本,前进,方法选择:基本:加,减,前进:乘,除和模。 currently struck at issue where i select class as basic, it shows basic in method selection dropdown also which is not inteded.目前遇到的问题是我 select class 是基本的,它在方法选择下拉列表中也显示基本,这也不是inteded。 thereafter if i select class as basic and in method i select add, class which has to be basic is also getting changed to add.此后,如果我将 select class 作为基本并且在方法中我 select 添加,ZA2F2ED4F8EBC0 更改为基本的添加到 CBB614C21A29DC。 help me out.帮帮我。

You have used same tkinter variable self.combobox for both Combobox widgets.您为两个Combobox小部件使用了相同的 tkinter 变量self.combobox

Remove the line:删除行:

self.TCombobox2.configure(textvariable=self.combobox)

Or use another tkinter variable for self.TCombobox2 .或者为self.TCombobox2使用另一个 tkinter 变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM