如何获取用户选择的颜色并将其用作tkinter框架的背景颜色?
list2 = ["red", "red", "red", "red", "blue", "yellow"];
droplist = OptionMenu(root, c, *list2)
droplist.config(width=15)
c.set('select your colour')
droplist.place(x=240, y=320)
root.configure(bg=c)
答案 0 :(得分:3)
让我们通过填写一些缺失的片段来完成这项工作:
{
"name": "test",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "16.8.3",
"react-native": "0.59.9",
"react-native-gesture-handler": "^1.3.0",
"react-native-vector-icons": "^6.6.0",
"react-navigation": "^3.11.0",
"styled-components": "^4.3.2"
},
"devDependencies": {
"@babel/core": "^7.5.0",
"@babel/runtime": "^7.5.1",
"babel-jest": "^24.8.0",
"jest": "^24.8.0",
"metro-react-native-babel-preset": "^0.55.0",
"react-test-renderer": "16.8.3"
},
"jest": {
"preset": "react-native"
}
}
主要缺少的部分是与import tkinter as tk
COLORS = ["red", "blue", 'green', 'cyan', 'magenta', "yellow"]
def change_color(*args):
root.configure(bg=color.get())
root = tk.Tk()
root.minsize(width=200, height=200)
color = tk.StringVar(root)
color.trace('w', change_color)
color.set(COLORS[0])
om = tk.OptionMenu(root, color, *COLORS)
label = tk.Label(root, text='Select your color')
om.pack(side="top")
label.pack(side="top")
root.mainloop()
关联的StringVar
,您可以通过它进行询问。要将回调函数与OptionMenu
关联,我们 trace 更改为OptionMenu
。