简体   繁体   English

如何调用此tkinter gui函数?

[英]How to call this tkinter gui function?

How do i access windows function in Mygui class? 如何访问Mygui类中的Windows函数? I want to create colored content in my tkinter box# but i can't pass value to it. 我想在我的Tkinter Box#中创建彩色内容,但是我无法将值传递给它。

      from tkinter import *
            class Mygui:
                def window(self, colour):
                    self.main_window=Tk()
                    self.main_window.geometry('300x100')
                    self.main_window.title('Login')
                    self.top_frame=Frame(self.main_window)
                    self.top_frame.pack()
                    self.label=Label(self.top_frame, fg=colour, text="Sample Text", width=45)
                    self.label.pack(side="top")
                    self.label1=Label(self.top_frame,text=" ", width=45)
                    self.label1.pack(side="top")
                    self.my_button = Button(self.main_window, text="Retry", command=self.do_something,  height=2, width=18)
                    self.my_button.pack()

                    mainloop()
                def do_something(self):
                    print('ok')

            class login:
                def example(self):
                    print("Start")
                    Mygui.window('blue')

            a = login.example(' ')

The error i get is: 我得到的错误是:

  Start
  Traceback (most recent call last):
  File "B:/data/loginMech/test.py", line 25, in <module>
  a = login.example(' ')
  File "B:/data/loginMech/test.py", line 23, in example
  Mygui.window('blue')
  TypeError: window() missing 1 required positional argument: 'colour'

Mygui is a class, not a function . Mygui是一个类,而不是一个函数 So, you have to construct an instance of it, like this: 因此,您必须构造一个实例,如下所示:

gui = Mygui()

And then you can call methods on that instance: 然后您可以在该实例上调用方法:

gui.window('blue')

When you write Mygui.window , that's an unbound method, which you can call by explicitly passing it a self argument along with its other arguments. 当您编写Mygui.window ,这是一个未绑定的方法,可以通过将其self参数以及其他参数显式传递给它来调用该方法。 But you'd still need to have something to pass as that self : 但是你仍旧需要有东西来传递作为self

gui = Mygui()
Mygui.window(gui, 'blue')

In general, you don't want to do this. 通常,您不想这样做。 There are cases where unbound methods are useful, but if you have one, you probably know you have one. 在某些情况下,未绑定方法很有用,但是如果有一种方法,您可能知道您有一种。


And you need to do the same thing with login : 而且您需要对login执行相同的操作:

log = login()
log.example()

By calling login.example , you're using an unbound method again. 通过调用login.example ,您将再次使用未绑定的方法。 And then you're passing ' ' as the self argument. 然后,您将传递' '作为self变量。 This doesn't make any sense, because ' ' is not a login instance, but CPython 3.x happens to not check for that error, so you get away with it. 这没有任何意义,因为' '不是login实例,但是CPython 3.x碰巧没有检查该错误,因此您可以摆脱它。

Abarnet pointed out one fix however in Tkinter it might be better to inherit from the Tkinter class Tk to start you GUI. Abarnet指出了一个修复程序,但是在Tkinter中最好继承自Tkinter类Tk以启动GUI。

Take this example. 举这个例子。 A much smaller amount of code and produces the same results desired. 更少的代码并产生所需的相同结果。

import tkinter as tk

class MyGUI(tk.Tk):
    def __init__(self, colour):
        tk.Tk.__init__(self)
        self.geometry('300x100')
        self.title('Login')
        tk.Label(self, fg=colour, text="Sample Text", width=45, pady=10).grid(row=0, column=0)
        tk.Button(self, text="Retry", command=self.do_something, height=2, width=18).grid(row=1, column=0)

    def do_something(self):
        print('ok')


MyGUI("Blue").mainloop()

Results: 结果:

在此处输入图片说明

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

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