首页 > 解决方案 > 将 2 个 python GUI 菜单合并在一起

问题描述

我在 2 个不同的脚本中有 2 个不同的 python GUI 菜单。

  1. 要选择的下拉列表
  2. 可以在同一个 GUI 上输入和输出数字的菜单

我想将这两个合并在一起,以便它们出现在同一个 GUI 菜单中。我在做这件事时遇到了麻烦,因为两个 python 脚本中的结构不同。请指教。

共享以下两个脚本:

第一个

from tkinter import *


class Custombox:
    def __init__(self, title, text):
        self.title = title
        self.text = text

        def store():
            self.new = self.entry.get()  # storing data from entry box onto variable
            if self.new == '50':  # checking
                a.change('ACCEPTED')  # changing text
            elif self.new == '40':
                a.change('Increase frequency')  # else, changing text
            else:
                a.change('Decrease frequency')  # else, changing text

        self.win = Toplevel()
        self.win.title(self.title)
        # self.win.geometry('400x150')
        self.win.wm_attributes('-topmost', True)

        self.label = Label(self.win, text=self.text)
        self.label.grid(row=0, column=0, pady=(20, 10), columnspan=3, sticky='w', padx=10)

        self.l = Label(self.win)

        self.entry = Entry(self.win, width=50)
        self.entry.grid(row=1, column=1, columnspan=2, padx=10)


        self.b1 = Button(self.win, text='Attack', width=10, command=store)
        self.b1.grid(row=3, column=1, pady=10)

        # self.b2 = Button(self.win, text='Cancel', width=10, command=self.win.destroy)
        # self.b2.grid(row=3, column=2, pady=10)

    def __str__(self):
        return str(self.new)

    def change(self, ran_text):
        self.l.config(text=ran_text, font=(0, 12))
        self.l.grid(row=2, column=1, columnspan=3, sticky='nsew', pady=5)


root = Tk()
root.withdraw()

a = Custombox('User learning platform', 'Select the frequency of the victim sensor.')

root.mainloop()

第二个

from tkinter import *


root = Tk()
root.title("GUI platform")

# Add a grid
mainframe = Frame(root)
mainframe.grid(column=0,row=0, sticky=(N,W,E,S) )
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
mainframe.pack(pady = 100, padx = 100)

# Create a Tkinter variable
tkvar = StringVar(root)

# Dictionary with options
choices = { 'URM37','HC-SR04','SRF05','Parallax PING'}
tkvar.set('') # set the default option

popupMenu = OptionMenu(mainframe, tkvar, *choices)
Label(mainframe, text="Please select the type of Sensor for attack").grid(row = 1, column = 1)
popupMenu.grid(row = 2, column =1)


# on change dropdown value
def change_dropdown(*args):
    if tkvar.get() == 'HC-SR04':
        print( "Correct" )

    else:
        print("WRONG")
# link function to change dropdown
tkvar.trace('w', change_dropdown)

root.mainloop()

标签: pythontkintermerge

解决方案


推荐阅读