10

我有一个 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()

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

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

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

两个问题:

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

  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 等。

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

4

2 回答 2

11

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

要删除多余的空间,请删除多余的 "\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()
于 2009-12-04T07:33:09.087 回答
0

这是一个用于滚动帧的类。只需将窗口对象作为传统的 tkinter 样式传递,并使用 obj.frame 作为新窗口小部件的窗口。

class ScrollableFrame:
    """
    # How to use class
    from tkinter import *
    obj = ScrollableFrame(master,height=300 # Total required height of canvas,width=400 # Total width of master)
    objframe = obj.frame
    # use objframe as the main window to make widget
    """
    def __init__ (self,master,width,height,mousescroll=0):
        self.mousescroll = mousescroll
        self.master = master
        self.height = height
        self.width = width
        self.main_frame = Frame(self.master)
        self.main_frame.pack(fill=BOTH,expand=1)

        self.scrollbar = Scrollbar(self.main_frame, orient=VERTICAL)
        self.scrollbar.pack(side=RIGHT,fill=Y)

        self.canvas = Canvas(self.main_frame,yscrollcommand=self.scrollbar.set)
        self.canvas.pack(expand=True,fill=BOTH)

        self.scrollbar.config(command=self.canvas.yview)

        self.canvas.bind('<Configure>', lambda e: self.canvas.configure(scrollregion = self.canvas.bbox("all")))

        self.frame = Frame(self.canvas,width=self.width,height=self.height)
        self.frame.pack(expand=True,fill=BOTH)
        self.canvas.create_window((0,0), window=self.frame, anchor="nw")

        self.frame.bind("<Enter>", self.entered)
        self.frame.bind("<Leave>", self.left)

    def _on_mouse_wheel(self,event):
        self.canvas.yview_scroll(-1 * int((event.delta / 120)), "units")

    def entered(self,event):
        if self.mousescroll:
            self.canvas.bind_all("<MouseWheel>", self._on_mouse_wheel)
        
    def left(self,event):
        if self.mousescroll:
            self.canvas.unbind_all("<MouseWheel>")
于 2021-08-08T14:15:06.967 回答