使函数运行cod

2024-03-29 11:03:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python和Tkinter制作一个应用程序。假设我添加了两个按钮,一个是MAIN,一个是NEWS,当我按下MAIN时,让函数mainthumsfun运行并设置变量,然后用新变量运行gui函数。我该怎么做呢?在

import StringIO
import Scraper
import Tkinter as tk
from PIL import Image, ImageTk

root = tk.Tk()
root.title('RazeTheWorld')

maintumbs = Scraper.maintumbs()
newstumbs = Scraper.newstumbs()

def mainthumsfun():
    url0 = mainthumbs[0]
    url1 = mainthumbs[1]
    url2 = mainthumbs[2]
    url3 = mainthumbs[3]

def newsthumbsfun():
    url0 = newsthumbs[0]
    url1 = newsthumbs[1]
    url2 = newsthumbs[2]
    url3 = newsthumbs[3]

def gui():
    imgf1 = urllib.urlopen(url0)
    imgwr1 = StringIO.StringIO(imgf1.read())
    image1 = ImageTk.PhotoImage(Image.open(imgwr1))
    panel1 = tk.Label(root, image=image1)
    panel1.grid(row=0,column=0)

    imgf2 = urllib.urlopen(url1)
    imgwr2 = StringIO.StringIO(imgf2.read())
    image2 = ImageTk.PhotoImage(Image.open(imgwr2))
    panel2 = tk.Label(root, image=image2)
    panel2.grid(row=1,column=0)

    imgf3 = urllib.urlopen(url2)
    imgwr3 = StringIO.StringIO(imgf3.read())
    image3 = ImageTk.PhotoImage(Image.open(imgwr3))
    panel3 = tk.Label(root, image=image3)
    panel3.grid(row=2,column=0)

    imgf4 = urllib.urlopen(url4)
    imgwr4 = StringIO.StringIO(imgf4.read())
    image4 = ImageTk.PhotoImage(Image.open(imgwr4))
    panel4 = tk.Label(root, image=image4)
    panel4.grid(row=3,column=0)

root.mainloop()

Tags: imageimportreadrootopenurlliblabeltk
1条回答
网友
1楼 · 发布于 2024-03-29 11:03:06

你只想要点击时运行代码的按钮? 您要做的是在根Frame内绘制一个小部件,例如按钮或菜单字段。 Check out this example text editor

它是一个文本编辑器,有一个菜单和几个按钮,当你点击它时,它会调用一个方法。不会了。易于摸索:)

相关问题 更多 >