【问题标题】:Tkinter objects are not centeringTkinter 对象没有居中
【发布时间】:2020-07-24 16:17:08
【问题描述】:

我正在尝试使用 tkinter 创建一个简单的 Python 3.x GUI 项目,只是为了更好地学习该语言(我不久前开始学习 Python),其中唯一的作用就是在不同页面之间切换为按钮被点击。问题是对象没有进入屏幕中心。我的代码有什么问题?

图片:

Start Page

First Page

Second Page

import tkinter as tk

LARGE_FONT = ("verdana", 10)


class Application(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.grid()

        self.frames = {}

        for F in (StartPage, PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="snew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class StartPage (tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label1 = tk.Label(self, text="Start Page", font=LARGE_FONT)
        label1.grid(row=0, column=0)

        button1 = tk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame(PageOne))
        button1.grid(row=1, column=0)

        button2 = tk.Button(self, text="Go to Page Two",
                            command=lambda: controller.show_frame(PageTwo))
        button2.grid(row=2, column=0)


class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label2 = tk.Label(self, text="Page One", font=LARGE_FONT)
        label2.grid(row=0, column=0, sticky="E")

        button2 = tk.Button(self, text="Go to Page Two",
                            command=lambda: controller.show_frame(PageTwo))
        button2.grid(row=1, column=0)

        button3 = tk.Button(self, text="Go to Start Page",
                            command=lambda: controller.show_frame(StartPage))
        button3.grid(row=2, column=0)


class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label2 = tk.Label(self, text="Page Two", font=LARGE_FONT)
        label2.grid(row=0, column=0,sticky="E")

        button2 = tk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame(PageOne))
        button2.grid(row=1, column=0)

        button3 = tk.Button(self, text="Go to Start Page",
                            command=lambda: controller.show_frame(StartPage))
        button3.grid(row=2, column=0)


app = Application()
app.title("Application")
app.mainloop()

【问题讨论】:

  • 你从一个太复杂的例子开始。我建议您首先创建一个只有一个页面的程序。在您尝试同时处理多个页面之前,先让它发挥作用。
  • 感谢您的回复!实际上,我之前已经做了一些简单的项目,只有一页,我觉得准备好尝试一些更难的东西了。我了解 OOP 和 tkinter 的基础知识,所以我开始了我在 youtube 教程中看到的这个新项目,但我仍然无法解决中心化问题,真的很难解决吗?

标签: python-3.x oop user-interface tkinter


【解决方案1】:

我用 tkinter 在左边放了一个菜单。菜单右侧的所有内容都在中间对齐。如何对齐到菜单的右侧

文本框背面有文字,但是因为是居中对齐,所以越过了文字,文字没有出现。

Tkinter program

所有代码:

from tkinter import *
from tkinter import messagebox
import tkinter.font as font
import tkinter as tkMessageBox
from typing import Sized
from PIL import ImageTk, Image  

window = Tk()
window.title("Ogretmen+")
window.geometry("900x447")
window.configure(bg="#202020")

genelgorunumbaslik = font.Font(size=20)
menuyazilar = font.Font(size=20)
guncellemebuyuk = font.Font(size=15)

def sinifYonetimi():
    sinifyonetimiaciklama.config(text="Sınıf yönetimi kısmına hoş geldiniz. Bu bölümden sınıf ekleyebilir ve silebilirsiniz. Örnek sınıf adı: 10 B")
    sinifyonetimiaciklama.grid(row=1, column=2)

    sinifadigir.config(text="Sınıf Adı:")
    sinifadigir.grid(row=2, column=2)
    sinifadientry.grid(row=2, column=2)
    islemsec.grid_remove()

sinifyonetimiaciklama = Label(fg="white", bg="#202020")
sinifadigir = Label(fg="white", bg="#202020")
sinifadientry = Entry(bd=4)

menubuton2 = Button(text="Sınıf Yönetimi", fg="white", bg="#282528", height=3, command=sinifYonetimi)
menubuton3 = Button(text="Öğrenci Yönetimi", fg="white", bg="#282528", height=3)
menubuton4 = Button(text="Konu Takibi", fg="white", bg="#282528", height=3)
menubuton5 = Button(text="İletişim", fg="white", bg="#282528", height=2)

menubuton2['font'] = menuyazilar
menubuton3['font'] = menuyazilar
menubuton4['font'] = menuyazilar
menubuton5['font'] = menuyazilar


menubuton2.grid(row=1, column=1, sticky="ew")
menubuton3.grid(row=2, column=1, sticky="ew")
menubuton4.grid(row=3, column=1, sticky="ew")
menubuton5.grid(row=4, column=1, sticky="ew")

islemsec = Label(text="Lütfen sol menüden işlem seçiniz.", bg="#202020", fg="white")
islemsec.grid(row=1, column=3)

window.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2023-02-02
    • 2019-08-22
    • 1970-01-01
    相关资源
    最近更新 更多