小部件不会显示出来。

huangapple go评论100阅读模式
英文:

Widgets wont Show Up

问题

我正在尝试创建一个非常简单的应用程序,所以概念是:我有一个主窗口,在窗口内部,我想要有2个框架,分别是"Options Frame"和"Content Frame",所以"Options Frame"将包含按钮,当按钮被点击时,"Content Frame"中的内容将会改变。我的问题是,"create_options_frame"中的按钮不会显示出来,但我没有收到任何错误消息,请帮助我。

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()

        self.geometry('800x300')
        self.title('Catatanku')

class OptionsView(ttk.Frame):
    def __init__(self, master, **kw):
        super().__init__(master, **kw)

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(1, weight=1)

        self.create_options_frame().grid(row=0, column=0, sticky='wns')
        self.create_content_frame().grid(row=0, column=1)

    def create_options_frame(self) -> ttk.Frame:
        self.options_frame = ttk.Frame(self )
        btn_buat_catatan = tk.Button(self.options_frame, text='Buat Catatan', font=('Times new roman', 16))
        btn_buat_catatan.pack(fill=tk.BOTH, expand=True)

        btn_buka_catatan = tk.Button(self.options_frame, text='Buka Catatan', font=('Times new roman', 16))
        btn_buka_catatan.pack(fill=tk.BOTH, expand=True)

        return self.options_frame 
        
    def create_content_frame(self) -> ttk.Frame:
        self.content_frame = ttk.Frame(self)
        return self.content_frame

if __name__=='__main__':
    root = tk.Tk()
    root.mainloop()
英文:

im trying to make a very simple app, so the concept is: i have a main window, and inside the window i want have 2 frames which are Options Frame and Content Frame, so the Options Frame will contain Buttons, when a button is clicked, the content in Content Frame will change. My problem is, buttons on def create_options_frame wont show up, but i don't get any error message, please help ,me

import tkinter as tk
from tkinter import ttk
from tkinter import messagebox

class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()

        self.geometry('800x300')
        self.title('Catatanku')

class OptionsView(ttk.Frame):
    def __init__(self, master, **kw):
        super().__init__(master, **kw)

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(1, weight=1)

        self.create_options_frame().grid(row=0, column=0, sticky='wns')
        self.create_content_frame().grid(row=0, column=1)

    def create_options_frame(self) -> ttk.Frame:
        self.options_frame = ttk.Frame(self )
        btn_buat_catatan = tk.Button(self.options_frame, text='Buat Catatan', font=('Times new roman', 16))
        btn_buat_catatan.pack(fill=tk.BOTH, expand=True)

        btn_buka_catatan = tk.Button(self.options_frame, text='Buka Catatan', font=('Times new roman', 16))
        btn_buka_catatan.pack(fill=tk.BOTH, expand=True)

        return self.options_frame 
        

    def create_content_frame(self) -> ttk.Frame:
        self.content_frame = ttk.Frame(self)
        return self.content_frame


if __name__=='__main__':
    root = tk.Tk()
    root.mainloop()

答案1

得分: 2

我认为你需要的是:

  • 创建MainWindow的实例,而不是tk.Tk的实例。
  • MainWindow内创建OptionsView的实例。
import tkinter as tk
from tkinter import ttk

class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()

        self.geometry('800x300')
        self.title('Catatanku')

        # 创建OptionsView的实例
        self.view = OptionsView(self)
        self.view.pack(fill="both", expand=1)

class OptionsView(ttk.Frame):
    def __init__(self, master, **kw):
        super().__init__(master, **kw)

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(1, weight=1)

        self.create_options_frame().grid(row=0, column=0, sticky='wns')
        self.create_content_frame().grid(row=0, column=1)

    def create_options_frame(self) -> ttk.Frame:
        self.options_frame = ttk.Frame(self)
        btn_buat_catatan = tk.Button(self.options_frame, text='Buat Catatan', font=('Times new roman', 16))
        btn_buat_catatan.pack(fill=tk.BOTH, expand=True)

        btn_buka_catatan = tk.Button(self.options_frame, text='Buka Catatan', font=('Times new roman', 16))
        btn_buka_catatan.pack(fill=tk.BOTH, expand=True)

        return self.options_frame

    def create_content_frame(self) -> ttk.Frame:
        self.content_frame = ttk.Frame(self)
        return self.content_frame

if __name__=='__main__':
    # 创建MainWindow的实例,而不是tk.Tk的实例
    root = MainWindow()
    root.mainloop()
英文:

I think what you need is:

  • create instance of MainWindow instead of tk.Tk
  • create instance of OptionsView inside MainWindow
import tkinter as tk
from tkinter import ttk

class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()

        self.geometry('800x300')
        self.title('Catatanku')

        # create instance of OptionsView
        self.view = OptionsView(self)
        self.view.pack(fill="both", expand=1)

class OptionsView(ttk.Frame):
    def __init__(self, master, **kw):
        super().__init__(master, **kw)

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(1, weight=1)

        self.create_options_frame().grid(row=0, column=0, sticky='wns')
        self.create_content_frame().grid(row=0, column=1)

    def create_options_frame(self) -> ttk.Frame:
        self.options_frame = ttk.Frame(self)
        btn_buat_catatan = tk.Button(self.options_frame, text='Buat Catatan', font=('Times new roman', 16))
        btn_buat_catatan.pack(fill=tk.BOTH, expand=True)

        btn_buka_catatan = tk.Button(self.options_frame, text='Buka Catatan', font=('Times new roman', 16))
        btn_buka_catatan.pack(fill=tk.BOTH, expand=True)

        return self.options_frame


    def create_content_frame(self) -> ttk.Frame:
        self.content_frame = ttk.Frame(self)
        return self.content_frame


if __name__=='__main__':
    # create instance of MainWindow instead of tk.Tk
    root = MainWindow()
    root.mainloop()

huangapple
  • 本文由 发表于 2023年8月9日 15:08:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865381-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定