有没有办法在python中扩展GUI框?

2024-06-16 10:30:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个简单的句子生成器。代码可以工作,但我只想知道是否有任何方法可以扩展GUI输入框?你知道吗

它看起来是这样的:

enter image description here

任何帮助都将不胜感激。你知道吗

import tkinter as tk
from tkinter import Entry
import random
window = tk.Tk()

def movingVerb():
    verbs = ["goes to", "walks through", "runs through"]
    moveVerb = random.choice(verbs)
    return moveVerb
def anObject():
    anObject = objectEntry.get()
def Generate():
    UserName = nameEntry.get()
    moveVerb = movingVerb()
    place = placeEntry.get()
    noun = objectEntry.get()
    sentence = UserName + " " + moveVerb + " a " + place + " and finds a " + noun + " ." 
    result.delete(0, tk.END)
    result.insert(0, sentence)

UserNameLabel = tk.Label(window, text="Enter a name: ")
nameEntry = tk.Entry(window)
anObjectLabel = tk.Label(window,text="Enter an object: ")
objectEntry = tk.Entry(window)
placeLabel = tk.Label(window, text="Enter a place: ")
placeEntry = tk.Entry(window)
button = tk.Button(window, text="Generate story", command=Generate)
result = tk.Entry(window)

UserNameLabel.pack()
nameEntry.pack()
anObjectLabel.pack()
objectEntry.pack()
placeLabel.pack()
placeEntry.pack()
button.pack()
result.pack()
window.mainloop()


Tags: textimportgetdefplaceresultwindowgenerate
2条回答

用地方而不是包装,你可以在这里做这个甜蜜的方法。你知道吗

 placeLabel.place(relwidth=0.25, relheight=0.90)

如果要在调整窗口大小时展开窗口小部件,则可以在pack()中以不同的组合使用expandfill。这一切都取决于你想扩展的元素和方向。你知道吗

.pack(fill='x')
.pack(fill='y')
.pack(fill='both')
.pack(expand=True, fill='x')
.pack(expand=True, fill='y')
.pack(expand=True, fill='both')

enter image description here


如果要调整窗口大小以查看Entry中的所有新文本,则可以使用widget.configure(width=...)。条目使用字符数作为宽度。你知道吗

你可以用句子的长度来改变宽度

result.configure(width=len(sentence))

或者保持宽度至少20个字符

length = max(20, len(sentence))
result.configure(width=length)

完整代码

import tkinter as tk
import random

#  - functions  -

verbs = ["goes to", "walks through", "runs through"]

def generate():
    user_name = name_entry.get()
    move_verb = random.choice(verbs)
    place = place_entry.get()
    noun = object_entry.get()
    sentence = '{} {} a {} and finds a {}.'.format(user_name, move_verb, place, noun)
    result.delete(0, 'end')
    result.insert(0, sentence)
    length = max(20, len(sentence))
    result.configure(width=length)

#  - main  -

window = tk.Tk()

user_name_label = tk.Label(window, text="Enter a name: ")
name_entry = tk.Entry(window)
object_label = tk.Label(window,text="Enter an object: ")
object_entry = tk.Entry(window)
place_label = tk.Label(window, text="Enter a place: ")
place_entry = tk.Entry(window)
button = tk.Button(window, text="Generate story", command=generate)
result = tk.Entry(window)

user_name_label.pack(expand=True, fill='both')
name_entry.pack(fill='both')
object_label.pack(expand=True, fill='both')
object_entry.pack(fill='both')
place_label.pack(expand=True, fill='both')
place_entry.pack(fill='both')
button.pack(expand=True, fill='both')
result.pack(fill='both')

window.mainloop()

相关问题 更多 >