简体   繁体   English

Tkinter:滚动条显示但不起作用

[英]Tkinter: Scrollbar shows but does not work

I'm trying to create a window to display a dataframe. 我正在尝试创建一个窗口来显示数据框。 However, the dataframe is way too big so I want to add scrollbars to be able to see the data. 但是,数据框太大,因此我想添加滚动条以能够查看数据。 The problem is the scrollbars appear but don't work. 问题是滚动条出现但不起作用。 I have tried several solutions I found here but none of these seem to work for me. 我尝试了在这里找到的几种解决方案,但这些解决方案似乎都不适合我。

Here is my code: 这是我的代码:

import tkinter as tk
root = tk.Tk()
#create frame
frame = tk.Frame(root)
frame.grid(sticky = 'news')
frame.grid_propagate(False)

#create Canvas
canvas = tk.Canvas(frame, bg = 'white')
canvas.grid(row = 0, column = 0)

#scrollbars
yscrollbar = tk.Scrollbar(frame, orient = tk.VERTICAL, command = canvas.yview)
yscrollbar.grid(row = 0, column = 1, sticky = tk.NS)
canvas.configure(yscrollcommand=yscrollbar.set)
xscrollbar = tk.Scrollbar(frame, orient = tk.HORIZONTAL, command = canvas.xview)
xscrollbar.grid(row = 1, column = 0, sticky = tk.EW)
canvas.configure(xscrollcommand=xscrollbar.set)

#new frame to contain text
frame_text = tk.Frame(canvas)
canvas.create_window((0, 0), window=frame_text, anchor='nw')

#insert dataframe
values = [['Abbreviation',  'UNIPROT Code', 'Full Name',    'Sample Type',  'Sample',   'Patients AGE±SD',  'Condition',    'Recurrence',   'Surgery',  'Patients Comorbidities',   'Patients (n)', 'Controls AGE±SD'],['MYL4', 'P12829',   'Myosin light chain 4', 'tissue',   'Left atrial appendages',   '68.2±2.9', 'permanent',    'n',    'mitral valve surgery', 'MR (11)',  '11.0', '58,8 ± 4,3'], ['AHSG', 'P02765',   'alpha2-HS glycoprotein',   'tissue',   'Right atrial appendages',  '72±10',    'permanent (5), persistent (2), paroxysmal (1)',    'n',    'CABG or valve surgery',    'Hypertension (6), CAD (4)',    '8.0',  '65±12']]

widgets = []
for row in range(len(values)):
    current_row = []
    for column in range(len(values[0])): 
        label = tk.Label(frame_text, text=values[row][column],borderwidth=0, width=len(str(values[row][column])))
        label.grid(row=row, column=column, sticky="nsew", padx=1, pady=1)
        current_row.append(label)
        widgets.append(current_row)

for column in range(len(values[0])):
    frame_text.grid_columnconfigure(column, weight=1)


#Resize the canvas frame
frame.config(width = 1000 + yscrollbar.winfo_width(),height=1000+ 
xscrollbar.winfo_width())

canvas.config(scrollregion=canvas.bbox("all"))

Also, how can I set the canvas size to be the same as the window? 另外,如何将画布大小设置为与窗口相同? Because what I get now is: picture 因为我现在得到的是: 图片

SOLVED 解决了

I realized the reason it wasn't working was because the scrollabe region was smaller than the canvas. 我意识到它无法正常工作的原因是,scrollabe区域比画布小。 So I changed: 所以我改变了:

frame.config(width = 1000 + yscrollbar.winfo_width(),height=1000 + xscrollbar.winfo_width())

canvas.config(scrollregion=canvas.bbox("all"))

to: 至:

frame.config(width = root.winfo_width(),height=root.winfo_height())

canvas.config(width = root.winfo_width()-yscrollbar.winfo_reqwidth()-10,height=root.winfo_height()-xscrollbar.winfo_reqheight()-10,scrollregion=(0,0,frame_text.winfo_reqwidth(),frame_text.winfo_reqheight()))

I've always assigned the command by configuring the scrollbars, not the canvas. 我总是通过配置滚动条而不是画布来分配命令。 Try replacing your lines: 尝试替换您的行:

canvas.configure...

With: 附:

yscrollbar.config(command=canvas.yview)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM