python 如何使用 tkinter 向窗口添加滚动条?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1844995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:12:05  来源:igfitidea点击:

How to add a scrollbar to a window with tkinter?

pythontkinter

提问by Andrew

I have a tkinter program:

我有一个 tkinter 程序:

import urllib.request
from tkinter import *


root = Tk()
root.iconbitmap(default='icon.ico')
root.wm_title('Got Skills\' Skill Tracker')
frame = Frame(width="500",height="500")
frame.pack()


def show():
  name = "zezima"
  page = urllib.request.urlopen('http://hiscore.runescape.com/index_lite.ws?player=' + name)
  page = page.readlines()

  skills = []
  for line in page:
    skills.append([line.decode("utf-8").replace("\n", "").split(",")])

  skills = skills[0:25]

  for item in skills:
    toPrint = item[0][0],"-",item[0][1],"-",item[0][1],"\n"
    w = Message(frame, text=toPrint)
    w.pack()


menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="Commands", menu=filemenu)
filemenu.add_command(label="Show Skills", command=show)


root.mainloop()

When I run the above script, it shows this (which is good):

当我运行上面的脚本时,它显示了这一点(这很好):

alt text http://img708.imageshack.us/img708/8821/tkinter1.png

替代文字 http://img708.imageshack.us/img708/8821/tkinter1.png

When I click Commands > Show Skills, it turns into this. (Linked because it's tall.) It shows the right thing, but...I can imagine you see the problem.

当我单击 Commands > Show Skills 时,它变成了这个. (链接是因为它很高。)它显示了正确的东西,但是......我可以想象你看到了问题。

Two questions:

两个问题:

-How do I add a scrollbar to the frame, and keep the frame a fixed size? (Ideally, keep the size of the first image, add the output of show(), add a scrollbar to the first image of the program.) -With the following code:

- 如何向框架添加滚动条,并保持框架的固定大小?(理想情况下,保持第一张图片的大小,添加 show() 的输出,在程序的第一张图片上添加滚动条。) - 使用以下代码:

  for item in skills:
    toPrint = item[0][0],"-",item[0][1],"-",item[0][2],"\n"
    w = Message(frame, text=toPrint)
    w.pack()

Is that the best way to output what I'm outputting? The list (skills) looks like [[1,2,3],[4,5,6]..], and I want to display 1-2-3 on a line, 4 - 5 - 6 on a line, etc.

这是输出我输出内容的最佳方式吗?列表 ( skills) 看起来像[[1,2,3],[4,5,6]..],我想在一行上显示 1-2-3,在一行上显示 4 - 5 - 6,等等。

But, I don't want that extra line in between them like there is now, and I was wondering if how I did it is the best way to go about doing it.

但是,我不希望像现在这样在它们之间出现额外的界线,我想知道我是如何做到的是否是最好的方法。

回答by abbot

To add the scroll bars, use tkinter.tix.ScrolledWindow.

要添加滚动条,请使用 tkinter.tix.ScrolledWindow。

To remove extra space drop the extra "\n" and display a string, not a tuple. Here is the complete code:

要删除额外的空间,请删除额外的 "\n" 并显示一个字符串,而不是一个元组。这是完整的代码:

import urllib.request
from tkinter import *
from tkinter.tix import *

root = Tk()
root.iconbitmap(default='icon.ico')
root.wm_title('Got Skills\' Skill Tracker')
frame = Frame(width="500",height="500")
frame.pack()
swin = ScrolledWindow(frame, width=500, height=500)
swin.pack()
win = swin.window


def show():
  name = "zezima"
  page = urllib.request.urlopen('http://hiscore.runescape.com/index_lite.ws?player=' + name)
  page = page.readlines()

  skills = []
  for line in page:
    skills.append([line.decode("utf-8").replace("\n", "").split(",")])

  skills = skills[0:25]

  for item in skills:
    toPrint = item[0][0],"-",item[0][1],"-",item[0][1]
    w = Message(win, text=' '.join(toPrint), width=500)
    w.pack()


menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="Commands", menu=filemenu)
filemenu.add_command(label="Show Skills", command=show)


root.mainloop()