有誰知道或知道如何通過從串列框中選擇一個專案并將其顯示到 frame_3 來顯示圖片?這是整個代碼供參考,下面提供的圖片應該是程式的預期輸出,謝謝,非常感謝任何見解。
from tkinter import *
from turtle import position, right
from PIL import Image, ImageTk
root = Tk()
root.title("LT_DelosSantosCristianJay")
root.geometry("500x200")
#FRAMES
frame_1 = Frame(root)
frame_1.grid(row = 0, column = 0, columnspan = 2)
frame_2 = Frame(root)
frame_2.grid(row = 1, column = 0,pady=(0,20))
frame_3 = Frame(root)
frame_3.grid(row = 1, column = 1)
continents = {
"Asia": ["Philippines", "Malaysia", "Taiwan", "Singapore", "Thailand", "India"],
"Africa": ["Algeria", "Angola", "Egypt", "Cape Verde", "Liberia", "Kenya", "Morocco", "Nigeria"],
"America": ["Venezuela", "Peru", "Jamaica", "United States", "Cuba", "Chile", "Argentina"],
"Europe": ["Russia", "Germany", "United Kingdom", "Italy", "Ukraine", "France", "Belgium"]
}
# Frame One
var = StringVar()
var.set('Select')
Radiobutton(frame_1, text="Asia", font=("Times", 12, "bold"), fg="blue", variable=var, value='Asia', borderwidth=1,
relief=SOLID, width=10, command=lambda: RadioSelected(var.get())).pack(side=LEFT)
Radiobutton(frame_1, text="Africa", font=("Times", 12, "bold"), fg="blue", variable=var, value="Africa", borderwidth=1,
relief=SOLID, width=11, command=lambda: RadioSelected(var.get())).pack(side=LEFT)
Radiobutton(frame_1, text="America", font=("Times", 12, "bold"), fg="blue", variable=var, value="America", borderwidth=1,
relief=SOLID, width=11, command=lambda: RadioSelected(var.get())).pack(side=LEFT)
Radiobutton(frame_1, text="Europe", font=("Times", 12, "bold"), fg="blue", variable=var, value="Europe", borderwidth=1,
relief=SOLID, width=11, command=lambda: RadioSelected(var.get())).pack(side=LEFT)
# Frame Two
# Listbox and Scrollbar
flag_listbox = Listbox(frame_2, font=("Times",15,"bold"),width=15,height=7)
flag_listbox.pack(side=LEFT, fill=BOTH)
flag_scrollbar = Scrollbar(frame_2)
flag_scrollbar.pack(side=RIGHT, fill=BOTH)
flag_listbox.config(yscrollcommand=flag_scrollbar.set)
flag_scrollbar.config(command=flag_listbox.yview)
# Radio Button click
def RadioSelected(Value):
flag_listbox.delete(0, END)
list_values = continents[Value]
for i in list_values:
flag_listbox.insert(END, i)
#Frame Three
#ASIA
Philippines = ImageTk.PhotoImage(Image.open("Asia/PH.png"))
#Malaysia = ImageTk.PhotoImage(Image.open("Asia/Malaysia.png"))
#Singapore = ImageTk.PhotoImage(Image.open("Asia/Singapore.png"))
#Taiwan = ImageTk.PhotoImage(Image.open("Asia/taiwan.png"))
#Thailand = ImageTk.PhotoImage(Image.open("Asia/Thailand.png"))
flagbox_1 = Label(frame_3,bg="white",width=320,height=150,image=Philippines)
flagbox_1.grid(sticky="we",pady=(0,20))
#flagbox_2 = Label(frame_3,bg="white",width=320,height=150,image=Malaysia)
#flagbox_2.grid(sticky="we",pady=(0,20))
#flagbox_3 = Label(frame_3,bg="white",width=320,height=150,image=Singapore)
#flagbox_3.grid(sticky="we",pady=(0,20))
#flagbox_4 = Label(frame_3,bg="white",width=320,height=150,image=Taiwan)
#flagbox_4.grid(sticky="we",pady=(0,20))
#flagbox_5 = Label(frame_3,bg="white",width=320,height=150,image=Thailand)
#flagbox_5.grid(sticky="we",pady=(0,20))
root.mainloop()
uj5u.com熱心網友回復:
這是你可以做的。您可以更改專案的資料結構并遵循以下模式:
continents = {
"Continent 1": [
{'Country 1': image_for_country_1},
{'Country 2': image_for_country_2},
{'Country 3': image_for_country_3},
# Other countries
],
"Continent 2": [
{'Country 4': image_for_country_4},
{'Country 5': image_for_country_5},
{'Country 6': image_for_country_6},
# Other countries
],
# Other continents
}
因此,在您的情況下,字典必須以某種方式構造,例如:
Philippines = ImageTk.PhotoImage(Image.open("Asia/PH.png"))
Malaysia = ImageTk.PhotoImage(Image.open("Asia/Malaysia.png"))
# Define image for all the other countries
continents = {
"Asia": [
{"Philippines": Philippines},
{"Malaysia": Malaysia},
# Same pattern as above for other countries
],
"Africa": [
{"Algeria": Algeria},
{"Angola": Angola},
# Same pattern as above for other countries
],
# Same pattern as above for other continents
}
現在您將不得不更改將專案插入串列框的方式,因為您更改了字典結構:
def RadioSelected(Value):
flag_listbox.delete(0, END)
list_values = continents[Value]
for i in list_values:
flag_listbox.insert(END, list(i.keys())[0])
現在洗掉每個單獨的標志標簽,只有一個標志標簽/持有人:
flagbox = Label(frame_3,bg="white",width=320,height=150, image=Philippines)
flagbox.grid(sticky="we",pady=(0,20))
...現在您必須系結到串列框,以便在串列框中選擇一個專案時,您將運行一個更改影像的函式:
def callback(event):
continent = var.get() # Get the continent
country = flag_listbox.get(flag_listbox.curselection()) # Get the country
countries = continents[continent] # Get the list of countries in the selected continent
for each_country in countries: # Loop through the list
if country in each_country: # Check if the country exist in dictionary, then
img = list(each_country.values()) # Get the image from the dictionary
flagbox.config(image=img) # Update the image
break # Break the loop from going further
flag_listbox.bind('<<ListboxSelect>>',callback) # Bind the listbox
每次從串列框中選擇專案時,這應該能夠更改影像。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/488400.html
