如何添加串列框,嘗試了很多方法但仍然無法正常作業。如何添加串列框,嘗試了很多方法但仍然無法正常作業。如何添加串列框,嘗試了很多方法但仍然無法正常作業。
class App:
def __init__(self, root):
#setting title
root.title("-----")
#setting window size
width=600
height=500
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d %d %d' % (width, height, (screenwidth - width) / 2, (screenheight
- height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
List_box=tk.Listbox(root)
List_box["borderwidth"] = "1px"
ft = tkFont.Font(family='Times',size=10)
List_box["font"] = ft
List_box["fg"] = "#333333"
List_box["justify"] = "left"
List_box.place(x=50,y=130,width=192,height=293)
def imageupload_command(self):
global list
list = fun.runbot.selectfile()
app.List_box.insert('5555')
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
uj5u.com熱心網友回復:
您的代碼運行良好。

您可以在此處查看視窗中有一個串列框。
問題:
- 首先,您需要更改
List_box為self.List_box - 您沒有呼叫
imageupload_command函式在串列框中插入值 - 在此
app.List_box.insert('5555')您需要替換app為self. 您還需要在.insert方法中提供索引,例如self.List_box.insert(1,'5555')
import tkinter as tk
import tkinter.font as tkFont
class App:
def __init__(self, root):
#setting title
root.title("-----")
#setting window size
width=600
height=500
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d %d %d' % (width, height, (screenwidth - width) / 2, (screenheight
- height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
self.List_box=tk.Listbox(root)
self.List_box["borderwidth"] = "1px"
ft = tkFont.Font(family='Times',size=10)
self.List_box["font"] = ft
self.List_box["fg"] = "#333333"
self.List_box["justify"] = "left"
self.List_box.place(x=50,y=130,width=192,height=293)
def imageupload_command(self):
global list
list = fun.runbot.selectfile()
self.List_box.insert(1,'5555')
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
app.imageupload_command() # Calling the function.
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/497231.html
