按钮在tkinter中不可见

时间:2017-11-07 01:14:24

标签: python tkinter

我对tkinter非常新,需要一些指导。我试图在屏幕右下角创建三个并排的按钮,但它们没有出现在我的屏幕上。注释掉的部分是我的故障排除和尝试新事物。 matplotlib会有一个图形,需要叠加这些按钮,这样你就可以循环访问数据并记录某些点。这就是我所拥有的:

from tkinter.ttk import Frame, Button, Style
from tkinter import *

import matplotlib
matplotlib.use("TkAgg")


    class Main(Frame):
        top = Tk()

        C = Canvas(top, bg="blue", height=700, width=700)

        def __init__(self):
            super().__init__()

            self.initUI()

        def initUI(self):
            self.master.title("Project Astronomical-Yearly-Location-Apparatus")
            """self.pack(fill=BOTH, expand=1)"""
            self.centerWindow()
            self.master.title("Buttons")
            self.style = Style()
            self.style.theme_use("default")
            frame = Frame(self, relief=RAISED, borderwidth=1)
            frame.grid_columnconfigure(0, weight=1)
            frame.grid_rowconfigure(0, weight=1)

            self.master.title("Commence")

            menubar = Menu(self.master)
            self.master.config(menu=menubar)

            fileMenu = Menu(menubar)
            fileMenu.add_command(label="Side View", command=self.side_view)
            fileMenu.add_command(label="Bye Felicia", command=self.onExit)
            menubar.add_cascade(label="Commence", menu=fileMenu)

        def side_view(self):
            t = Toplevel(self)
            t.wm_title("Side View")
            l = Label(t, text="This is window")
            l.pack(side="top", fill="both", expand=True, padx=100, pady=100)

        def onExit(self):
            self.quit()

        def centerWindow(self):
            w = 700
            h = 700

            """self.pack(fill=BOTH, expand=True)"""

            nextButton = Button(self, text="Next", background="green")
            nextButton.grid(row=1, column=2)

            backButton = Button(self, text="Back", background="blue")
            backButton.grid(row=1, column=3)

            recordButton = Button(self, text="Record", background="red")
            recordButton.grid(row=1, column=1)

            self.master.resizable(width=False, height=False)
            sw = self.master.winfo_screenwidth()
            sh = self.master.winfo_screenheight()
            x = (sw - w) / 2
            y = (sh - h) / 2
            self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))


    def main():
        ex = Main()
        ex.mainloop()


    if __name__ == '__main__':
        main()

1 个答案:

答案 0 :(得分:0)

我以不同的方式导入了tkinter,这就是为什么我有“tk”。在Button和Menu对象之前(我认为通过这种方式更容易看到代码中发生了什么 - 你知道Button和Menu对象来自哪里)。我想这会做你想要的。请注意,在网格中放入更多内容之前,您将无法移动按钮。这里的下一步是向按钮添加命令以显示/更新图像。我建议把图像放在tk.Label中。那里有大量的文档,我自己也做了,所以我可以在将来提供帮助。

import tkinter as tk
import matplotlib.pyplot as plt

class Main(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("Program with three buttons")
        self.centerWindow()

        self.menubar = tk.Menu(self)
        self.fileMenu = tk.Menu(self.menubar,tearoff=0)
        self.fileMenu.add_command(label="Side View", command = self.side_view)
        self.fileMenu.add_command(label="Bye Felicia", command = self.onExit)

        self.menubar.add_cascade(label="Commence", menu=self.fileMenu)
        self.config(menu=self.menubar) # You don't have this line in the right spot, needs to be after you add_commands

    def side_view(self):
        t = tk.Toplevel(self)
        t.wm_title("Side View")
        l = tk.Label(t, text="This is window")
        l.pack(side="top",fill="both",expand=True,padx=100,pady=100)

    def onExit(self):
        self.destroy() # Changed to destroy

    def centerWindow(self):
        w = 700
        h = 700
        self.button1 = tk.Button(self, text="Button 1")
        self.button2 = tk.Button(self, text="Button 2")
        self.button3 = tk.Button(self, text="Button 3")
        self.button1.grid(row=1,column=1) 
        self.button2.grid(row=1,column=2)
        self.button3.grid(row=1,column=3)

        self.resizable(width=False, height=False)
        sw = self.winfo_screenwidth()
        sh = self.winfo_screenheight()
        x = (sw - w) / 2
        y = (sh - h) / 2
        self.geometry('%dx%d+%d+%d' % (w, h, x, y))    

main = Main()
main.mainloop()