我正在为我的电子项目应用程序工作一个简单的UI,我使用tkinter作为我的窗口,并想添加一个文本框,将被用作记录器。我的屏幕大小是窗口的初始大小,我希望文本框在窗口的右下角。
这是一个测试代码,我正在使用,试图找出它:
class Window:
def __init__(self, WIDTH, HEIGHT, WIN) -> None:
"""
Parameters
----------
self.width : int
The width of )the window created.
self.height : int
The height of the window created.
self.window : tk.TK
The window object.
Variables
---------
self.data : dict
A dictonary containing all information about buttons creates and titles displayed on screen.
"""
self.width = WIDTH
self.height = HEIGHT
self.window = WIN
self.data : dict = {
"Title" : None,
"btns" : {}
}
def constract(self, title : str, background_color : str) -> None:
"""
Creates the main window adds a title and a background color.
Parameters
----------
title : str
A string which will serve as the window's title.
backgound_color : str
A string represents a hex code color (e.g. #FFFFFF).
"""
self.window.title(title)
self.window.geometry(f'{self.width}x{self.height}')
self.window.configure(bg=background_color)
def header(self, text : str, fill : str = "black", font : str = 'Arial 28 bold', background : str ='#E1DCDC') -> None:
"""
Displays a title on the screen.
Parametes
---------
text : str
The text which will be displayed on the screen.
fill : str
The color of the text, can be an existing color in tkinter or a custom hex code color (e.g. #FFFFFF).
font : str
The font type, the size of the letters and other designs for the text.
backgound : str
The color of the box around the text, can be an existing color in tkinter or a custom hex code color (e.g. #FFFFFF).
"""
T = Label(self.window, text=text, bg=background ,fg=fill, font=font)
T.pack()
self.data["Title"] = text
class PrintLogger(): # create file like object
def __init__(self, textbox): # pass reference to text widget
self.textbox = textbox # keep ref
def write(self, text):
self.textbox.insert(tk.END, text) # write text to textbox
# could also scroll to end of textbox here to make sure always visible
def flush(self): # needed for file like object
pass
if __name__ == '__main__':
from tkinter import *
import sys
win = Tk()
WIDTH, HEIGHT = win.winfo_screenwidth(), win.winfo_screenheight()
main_window = Window(WIDTH, HEIGHT, win)
main_window.constract("AntiBalloons system controls", "#E1DCDC")
main_window.header("AntiAir system controls")
t = Text(win, bg="#E1DCDC",bd=1)
t.tag_configure("", justify='right')
t.pack()
pl = PrintLogger(t)
sys.stdout = pl
main_window.window.mainloop()
试试这个:
t.pack(side=BOTTOM, anchor='e')