python - 我似乎找不到将滚动条添加到我的 tkinter 元素周期表的方法
问题描述
import tkinter as tk
from tkinter import *
from tkinter import ttk
from PIL import Image,ImageTk
class App(tk.Frame):
def __init__(self, parent,*args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.Frame = ttk.Frame(self.parent)
tk.Text.__init__(self, self.Frame, width=1, height=1)
self.winfo_toplevel().title("Periodic Table of the Elements")
self.topLabel = tk.Label(self, text="Click the element", font=20)
self.topLabel.grid(row=0, column=0, columnspan=18)
self.__initWidget()
self.infoLine = tk.Label(self, text="", justify='left')
self.infoLine.grid(row=24, column=0, columnspan=10, rowspan=4)
#self.mainloop() #Comment or remove it
def scro(self,parent, *args,**kwargs):
self._y_scrollbar = tk.Scrollbar(self, orient=tk.VERTICAL)
self._text_widget = tk.Text(self, yscrollcommand=self._y_scrollbar.set, *args, **kwargs)
self._text_widget.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
# Replaces Label at the top with the name of whichever element tk.Button was pressed
def name(self, text):
self.topLabel.config(text=text)
# Displays information on the element of whichever element tk.Button was pressed
def info(self, text):
self.infoLine.config(text=text)
def scro(self):
root = Tk()
scrollbar = Scrollbar(root)
scrollbar.pack( side = RIGHT, fill = Y )
mylist.pack( side = LEFT, fill = BOTH )
scrollbar.config( command = mylist.yview )
def __initWidget(self):
self.Frame.grid(sticky="NSEW")
self.ScrollbarY = ttk.Scrollbar(self.Frame, orient="vertical",command=self.yview)
self.configure(yscrollcommand=self.ScrollbarY.set)
self.grid(column=0, row=0, sticky="NSEW")
self.ScrollbarY.grid(column=1, row=0, sticky="NS")
self.Frame.columnconfigure(0, weight=1)
self.Frame.rowconfigure(0, weight=1)
# Creates an instance of 'app' class
def main():
root = tk.Toplevel()
a = App(root)
a.grid(row=0, column=0, sticky='')
a.mainloop()
# runs main function
if __name__ == "__main__":
main()
我如何在其中添加滚动条,我已经删除了一些代码,主要是添加元素的循环,其他一切都还在。我尝试了各种方法,但似乎无法使其正常工作。我尝试将其打包在 def init中,但这不起作用,因为不能将网格和打包一起使用,我还尝试制作一个函数,它也显示在这里,但这不起作用。
解决方案
推荐阅读
- javascript - 在 clic 上打开另一个弹出窗口时关闭当前弹出窗口
- javascript - SapUi5 cookie 获取/设置
- .net - AWS SDK .NET Fargate ECS 获取当前任务的 ARN
- python - 如何使用带有编码头的xml文件的beautifulsoup解决问题
- jenkins - 如何在 Jenkins JCasC 插件的 .yaml 配置中定义多个可定位资源标签?
- android - 布局和drawable的居中方式不同
- hadoop - HBase MasterProcWALs 问题
- javascript - 我们可以在不使用上下旋转的情况下增加和减少 html 中数字框(在 forms 中)的值吗?
- python - 使用 python websockets 订阅多个频道
- java - 使用 PKCS11 密钥库类型在 Spring Boot 上的 Web 应用程序中建立 TLS 连接