【问题标题】:Issues with Tkinter in having buttons in the secondary (second) window by click button in main windowTkinter 通过单击主窗口中的按钮在辅助(第二个)窗口中具有按钮的问题
【发布时间】:2018-11-28 15:55:10
【问题描述】:

我现在能够生成一个代码,当我单击第一个窗口中的“测试”按钮时,它会弹出一个带有条目的新窗口。但是,我在第二个窗口中创建按钮时遇到问题。条目已创建,但没有按钮。另外一个问题,如果可能的话,我想补充一点,当我在第一个窗口中单击按钮测试时,会弹出第二个带有条目/文本/按钮的窗口并且第一个窗口被杀死?

按照我现在的方式,它向我弹出一个错误,说“'MySecondGUI' 对象没有属性 'buttonPressed2”

我们将不胜感激。

我在下面粘贴了我的代码:

GUI_VIEW 文件 (gui_view.py)

import tkinter as tk

from tkinter import Tk


class MyMainGUI(tk.Frame):

    def __init__(self, controller):

       tk.Frame.__init__(self)
       self.pack()
       self.controller = controller

       self.Button1=tk.Button(self)
       self.Button1["text"]= "Test"
       self.Button1["command"]=self.controller.buttonPressed1
       self.Button1.grid(row=2,column=0,rowspan=2)


class MySecondGUI(tk.Toplevel):

       def __init__(self):

        tk.Toplevel.__init__(self)


        self.outputLabel2 = tk.Label(self)
        self.outputLabel2["text"] = ("Enter Value")
        self.outputLabel2.grid(row=5,rowspan=2)

        self.entrySpace2 = tk.Entry(self)
        self.entrySpace2.grid(row=8,column=0,rowspan=2)

        self.Button2=tk.Button(self)
        self.Button2["text"]= "Try Me"
        self.Button2["command"] = self.buttonPressed2
        self.Button2.grid(row=14,column=0,rowspan=2)enter code here

GUI 主控制器文件

import tkinter as tk 

import gui_view # the VIEW file


class MainControl:

    def __init__(self):   
      self.root = tk.Tk()
      self.root.geometry('550x200')
      self.view = gui_view_temp.MyMainGUI(self)
      self.view.mainloop()

    def newWindow(self):

      self.viewNew = gui_view.MySecondGUI()
      self.viewNew.geometry('300x400')
      self.newDisplay = tk.Label(self.newWin, text='Test Mode')
      self.viewNew.mainloop()
      self.newDisplay.pack()

    def buttonPressed1(self):

         self.newWindow()

    def buttonPressed2(self):

         pass


if __name__ == "__main__":

    c = MainControl()

【问题讨论】:

  • 好吧,您的MySecondGUI 类没有属性self.buttonPressed2,您应该从MainControl 类中获取它,就像您为您的MyMainGUI 类所做的那样...
  • 嗨 Toti08,我没听懂。您能否详细说明并提供代码 sn-p。如何从 MainControl 类中获取它?问题是我不想同时弹出两个窗口。只有当 Button 1 被按下时,第二个窗口才会与 entry test/buttons 一起弹出。
  • 看看你得到的答案,你会找到解释。

标签: python pandas tkinter tkinter-canvas tkinter-layout


【解决方案1】:

您问题的第一部分

您的按钮没有出现,因为MySecondGUI 类也必须有权访问主根(“控制器”)。修改MySecondGUI的构造函数如下:

def __init__(self, controller):

    tk.Toplevel.__init__(self)
    self.controller = controller  # Add controller too

    self.outputLabel2 = tk.Label(self)
    self.outputLabel2["text"] = ("Enter Value")
    self.outputLabel2.grid(row=5, rowspan=2)

    self.entrySpace2 = tk.Entry(self)
    self.entrySpace2.grid(row=8, column=0, rowspan=2)

    self.Button2 = tk.Button(self)
    self.Button2["text"] = "Try Me"
    # Button referenced to the controller
    self.Button2["command"] = self.controller.buttonPressed2
    self.Button2.grid(row=14, column=0, rowspan=2)

然后,从主类调用它:

self.viewNew = gui_view.MySecondGUI(self)

您问题的第二部分

关于你的第二个问题,你不能杀死主窗口,因为它是你程序的根(第二个只是一个Toplevel 小部件)。您可以通过在函数newWindow 开头使用以下代码行来隐藏它:

self.root.withdraw()

如果你想再次使用根窗口(你应该),你可以使用deiconify方法:

self.root.deiconify()

【讨论】:

    猜你喜欢
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多