因此,我对Tkinter和整个Python还是很陌生。我正在一个项目,该项目从互联网上获取歌词,或者只是向您显示歌词,或者使用马尔可夫链来生成随机歌曲。我已经使代码工作了,我现在正尝试实现GUI。我已经到了可以输入歌曲和歌手的地步,程序会为您提供歌词。目前,歌词仍会在控制台中出现。我现在正在尝试将歌词导入GUI,但是当我尝试将其放在文本中时,该歌词已被删除。我在事物的tkinter部分使用了一个教程,所以我不确定代码的哪一部分是相关的。有问题的文本在GetLyrics类中。(lyric_label)当前仅使用“ example”作为占位符。提前非常感谢您,如果我在这篇文章中做错了什么,请告诉我。
这是问题的图片:
import get_lyrics
import textwrap
import tkinter as tk
from tkinter import ttk
from cc_markov import MarkovChain
lyric_data = MarkovChain()
LARGE_FONT = ("Verdana", 12)
small_font = ("Verdana", 10)
class lyrGenApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, "logo.ico")
tk.Tk.wm_title(self, "LyrGen")
tk.Tk.wm_geometry(self, '1024x576')
self.resizable(False, False)
container = tk.Frame(self)
container.pack(side="top", fill="both")
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, GenLyrics, GetLyrics):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
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)
label = tk.Label(self, text="Lyrgen", font=LARGE_FONT)
label.pack(pady=10, padx=10)
gen_button = ttk.Button(self, text="Generate Lyrics",
command=lambda:
controller.show_frame(GenLyrics))
gen_button.pack()
get_button = ttk.Button(self, text="Get Lyrics",
command=lambda:
controller.show_frame(GetLyrics))
get_button.pack()
class GenLyrics(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Generate Lyrics", font = LARGE_FONT)
label.pack(pady = 10, padx = 10)
button1 = ttk.Button(self, text="Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
class GetLyrics(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Get Lyrics", font = LARGE_FONT)
label.place(relx = 0.5, rely = 0.3, anchor = 's')
prompt_song = tk.Label(self, text = "Enter Song:", font = small_font)
prompt_song.place(relx = 0.38, rely = 0.6, anchor = 's')
prompt_artist = tk.Label(self, text = "Enter Artist:", font =
small_font)
prompt_artist.place(relx = 0.38, rely = 0.8, anchor = 's')
song_entry = tk.Entry(self)
song_entry.place(relx = 0.5, rely = 0.6, anchor = 's')
artist_entry = tk.Entry(self)
artist_entry.place(relx = 0.5, rely = 0.8, anchor = 's')
home_button = ttk.Button(self, text="Home",
command=lambda: controller.show_frame(StartPage))
home_button.place(relx = 0.1, rely = 0.7, anchor = 's')
lyric_label = tk.Label(self, text = "example", font = small_font,
justify = "left")
lyric_label.place(relx = .1, rely = .85)
lyrics = []
def get_button(list):
lyrics = list
print(" ".join(lyrics))
search_button = ttk.Button(self, text="Search", command= lambda:
get_button(get_lyrics.get_song(song_entry.get(),
artist_entry.get())))
search_button.place(relx = 0.6, rely = 0.7, anchor = 's')
app = lyrGenApp()
app.mainloop()
答案 0 :(得分:1)
将rely
的值减小为.8
或.75
或以下类似内容:
lyric_label.place(relx = .1, rely = .85)
更改为:
lyric_label.place(relx = .1, rely = .8)
或
lyric_label.place(relx = .1, rely = .75)