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

编程入门 行业动态 更新时间:2024-10-25 15:26:33
本文介绍了如何使用 tkinter 向窗口添加滚动条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个 tkinter 程序:

I have a tkinter program:

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/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):

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

当我点击命令 > 显示技能时,它变成了this.(链接是因为它很高.)它显示了正确的东西,但是......我可以想象你看到了问题.

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.

两个问题:

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

-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:

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

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

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.

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

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.

推荐答案

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

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

要删除多余的空间,请删除多余的\n"并显示字符串,而不是元组.完整代码如下:

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

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/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()

这篇关于如何使用 tkinter 向窗口添加滚动条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 08:18:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   滚动条   窗口   tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!