简体   繁体   English

在Tkinter中滚动浏览多个标签

[英]Scrolling through multiple labels in Tkinter

I am trying to make a simple application launcher using Tkinter ( for the games that I have made using pygame). 我正在尝试使用Tkinter创建一个简单的应用程序启动器(用于我使用pygame制作的游戏)。 The code for the same is below.It runs in full screen mode(without any maximize or minimize buttons). 相同的代码如下所示,它以全屏模式运行(没有任何最大化或最小化按钮)。

import Tkinter as tk
from Tkinter import *
import random
import os
import subprocess


def call(event,x):

   print "begin"
   if x==0:
       p = subprocess.Popen("python C:\Users\Akshay\Desktop\i.py")


   p.wait()
   print "end"



root = tk.Tk()
root.geometry("1368x768+30+30")
root.overrideredirect(True)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(),    root.winfo_screenheight()))

games = ['Game1','Game2','Game3','Game4','Game5','Game6','Game7','Game8']
labels = range(8)
for i in range(8):
    ct = [random.randrange(256) for x in range(3)]
    brightness = int(round(0.299*ct[0] + 0.587*ct[1] + 0.114*ct[2]))
    ct_hex = "%02x%02x%02x" % tuple(ct)
    bg_colour = '#' + "".join(ct_hex)
    l = tk.Label(root, 
            text=games[i], 
            fg='White' if brightness < 120 else 'Black', 
            bg=bg_colour)
    l.place(x = 320, y = 30 + i*150, width=700, height=100)
    l.bind('<Button-1>', lambda event, arg=i: call(event, arg))

root.mainloop()

There is no issue with this piece of code but I want a scroll bar at the right side or a way to scroll/move down using the arrow keys so that if I add more number of labels , they also become visible. 这段代码没有问题,但是我想要右侧的滚动条或使用箭头键向下滚动/向下移动的方式,这样,如果我添加更多数量的标签,它们也将变为可见。

I tried to understand some code snippets from the internet and read the Tkinter documentations as well but didn't understood anything. 我试图从互联网上理解一些代码片段,也阅读了Tkinter文档,但一无所知。 Also tried to follow one more stackoverflow discussion and understood about the frame and canvas methods. 还尝试遵循另外的stackoverflow讨论,并了解有关frame和canvas方法的知识。

Python Tkinter scrollbar for frame 框架的Python Tkinter滚动条

Frame , canvas and all is getting a bit complicated. 框架,画布和所有东西都变得有点复杂。 I just want to keep it simple.Can something be added to the code snippet above to make the scroll thing work and all the labels become visible? 我只是想保持简单,是否可以在上面的代码片段中添加一些内容以使滚动功能正常工作并且所有标签都可见?

在此处输入图片说明

Something like the above but with a scroll bar!! 像上面的东西,但带有滚动条!

Here is an MCVE of how to add a scrollbar to a tkinter app, hide the scrollbar, and scroll with the up/down arrows or the mouse wheel. 这是有关如何向tkinter应用程序添加滚动条,隐藏滚动条以及使用向上/向下箭头或鼠标滚轮滚动的MCVE

from tkinter import *

parent=Tk() # parent object
canvas = Canvas(parent, height=200) # a canvas in the parent object
frame = Frame(canvas) # a frame in the canvas
# a scrollbar in the parent
scrollbar = Scrollbar(parent, orient="vertical", command=canvas.yview)
# connect the canvas to the scrollbar
canvas.configure(yscrollcommand=scrollbar.set)
scrollbar.pack(side="right", fill="y") # comment out this line to hide the scrollbar
canvas.pack(side="left", fill="both", expand=True) # pack the canvas
# make the frame a window in the canvas
canvas.create_window((4,4), window=frame, anchor="nw", tags="frame")
# bind the frame to the scrollbar
frame.bind("<Configure>", lambda x: canvas.configure(scrollregion=canvas.bbox("all")))
parent.bind("<Down>", lambda x: canvas.yview_scroll(3, 'units')) # bind "Down" to scroll down
parent.bind("<Up>", lambda x: canvas.yview_scroll(-3, 'units')) # bind "Up" to scroll up
# bind the mousewheel to scroll up/down
parent.bind("<MouseWheel>", lambda x: canvas.yview_scroll(int(-1*(x.delta/40)), "units"))
labels = [Label(frame, text=str(i)) for i in range(20)] # make some Labels
for l in labels: l.pack() # pack them

parent.mainloop() # run program

This is a better answer to my question.Yes I know I can't make the scroll bars work properly but yes as I asked now through this code one can move down/scroll via arrow keys without a scroll bar actually being there. 这是对我的问题的更好回答。是的,我知道我无法使滚动条正常工作,但是是的,正如我现在通过这段代码询问的那样,可以通过箭头键向下/滚动,而实际上并没有滚动条。 This is the way I used to make menu for my pygame made games and the same technique I applied here.It works but only in fullscreen mode. 这是我用来为pygame制作的游戏制作菜单的方式,也是我在此处应用的相同技术。

import Tkinter as tk
from Tkinter import *
import random
import os
import subprocess

class stat:
   j=0

def call(event,x):

   print "begin"
   if x==0:
       p = subprocess.Popen("python C:\Users\Akshay\Desktop\i.py")


   p.wait()
   print "end"

def OnEntryDown(event):
    stat.j=stat.j+1
    print stat.j
    xmain()

def OnEntryUp(event):

     stat.j=stat.j-1
     print stat.j
     xmain()
def xmain():   
     root = tk.Tk()
     root.geometry("1368x768+30+30")
     root.overrideredirect(True)
     root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(),       root.winfo_screenheight()))
     root.bind("<Down>", OnEntryDown)
     root.bind("<Up>", OnEntryUp)


     languages =     ['Game1','Game2','Game3','Game4','Game5','Game6','Game7','Game8']
 labels = range(8)

     k=0
     print stat.j
     for i in range(stat.j,stat.j+5):
        ct = [random.randrange(256) for x in range(3)]
        brightness = int(round(0.299*ct[0] + 0.587*ct[1] + 0.114*ct[2]))
        ct_hex = "%02x%02x%02x" % tuple(ct)
        bg_colour = '#' + "".join(ct_hex)
        l = tk.Label(root, 
            text=languages[i], 
            fg='White' if brightness < 120 else 'Black', 
            bg=bg_colour)
        l.place(x = 320, y = 30 + k*150, width=700, height=100)
        l.bind('<Button-1>', lambda event, arg=stat.j: call(event, arg))
        k=k+1

     root.mainloop()

xmain()

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

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