为什么按钮没有出现在tkinter窗口中?

时间:2020-07-28 17:30:27

标签: python tkinter

我目前正在尝试学习tkinter。我不明白为什么我定义的按钮没有出现在此代码中:

import Foundation

// MARK: - Test
struct Test: Codable {
    let success: Bool
    let username: String
    let data: DataClass
}

// MARK: - DataClass
struct DataClass: Codable {
    let locations: Locations
    let market: Market
}

// MARK: - Locations
struct Locations: Codable {
    let asia: Asia
}

// MARK: - Asia
struct Asia: Codable {
    let japan, korea: Japan
}

// MARK: - Japan
struct Japan: Codable {
    let storeCount: Int

    enum CodingKeys: String, CodingKey {
        case storeCount = "store_count"
    }
}

// MARK: - Market
struct Market: Codable {
    let fruits: Fruits
}

// MARK: - Fruits
struct Fruits: Codable {
    let banana, apple, mango, peach: Apple
    let watermelon, blackberry: Apple
}

// MARK: - Apple
struct Apple: Codable {
    let price, count: Int
}

当我不使用课程时,它会起作用。例如:

from tkinter import *

class Window(Frame):
        def __init__(self, master=None):
            Frame.__init__(self, master)
            self.master = master



            button1 = Button(self, text="Exit", width=12, command=self.clickButton1)

            button1.grid(row=0)

            button2 = Button(self, text="Test", width=12, command=self.clickButton2)

            button2.grid(row=1)

        def clickButton1(self):
            exit()

        def clickButton2(self):
            print("Nice")

root = Tk()
app = Window(root)
root.title("Tkinter window")

root.mainloop()

1 个答案:

答案 0 :(得分:0)

原因是该类创建了一个框架,然后将小部件放置在该框架内。但是,您永远不要将框架添加到窗口中,因此框架内的小部件将不可见。

您需要确定并在pack的实例上调用gridplaceWindow,就像处理任何其他小部件一样。

app = Window(root)
app.pack(fill="both", expand=True)