首页 > 解决方案 > 来自 tkinter 和页面上的按钮的新窗口

问题描述

大家好,我正在使用页面为我的项目创建 gui;但是当我尝试使用按钮从窗口 1 打开窗口 2 时,将其作为参数 main 或 rot 在其中,它不会显示为错误 main未定义

在一个单独的python文件中,我有图形界面,在另一个python文件中,我有一个图形界面,必须通过按钮打开第一个文件中存在的另一个界面,我在第二个文件中导入了第一个文件的功能并读取创建函数;但是当我必须使用函数 create_toplevel1 将它作为参数作为第一个窗口退出的主窗口时,它告诉我它没有定义,对于 root 也是一样,并且没有参数是不可能的,因为函数必须接受一个参数

#first main when I click the button and appear the second window

class AdminPanel:
   def __init__(self, top=None):
       '''This class configures and populates the toplevel window.
          top is the toplevel containing window.'''
       self.btnregutenti =tk.Button(top,command=create_TopLevel1(root))

def create_AdminPanel(root, *args, **kwargs):
   '''Starting point when module is imported by another program.'''
   global w, w_win, rt
   rt = root
   w = tk.Toplevel (root)
   top = AdminPanel (w)
   return (w, top)


#second window that appear when I use a button

def create_TopLevel1(root, *args, **kwargs):
   '''Starting point when module is imported by another program.'''
   global w, w_win, rt
   rt = root
   w = tk.Toplevel (root)
   top = TopLevel1 (w)
  # user_interface_support.init(w, top, *args, **kwargs)
   return (w, top)

在本指令中 self.btnregutenti=tk.Button(top,command=create_TopLevel1(root))

我应该插入什么参数来显示正确的窗口?

标签: pythonbuttontkinter

解决方案


问题是 Button 将函数作为command=参数值。通过使用tk.Button(top,command=create_TopLevel1(root)),您已经在调用该函数。您应该改为使用tk.Button(top,command=create_TopLevel1)和编辑create_TopLevel1,因此它不需要任何参数。希望这会有所帮助!


推荐阅读