6

我对python真的很陌生,目前我正在尝试使用tkinter设计一个表单。我一直试图在笔记本中插入滚动条和表单,因为我没有找到问题的答案,而且很简单“如何在笔记本 tkinter 小部件中插入滚动条和表单?”...如您所见,这对您来说很简单,但对于像我这样的新手来说却不是!

但是,这是我到目前为止所做的,幸运的是它确实显示了滚动条,但是当我尝试将表单插入笔记本时它崩溃了!

注意:我的 python 版本是带有 EPD_free 7.3-2(32 位)的 Python 2.7.3

import Tkinter
from Tkinter import * 
from ttk import *   
import tkMessageBox 
import ttk  
import Tkinter as tk 

root = Tk()
root.title("Model_A")
root.resizable(0,0)

# start of Notebook (multiple tabs)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None

#Child Frames
ContainerOne = Frame(notebook)
ContainerOne.pack(fill=BOTH, expand=True)
ContainerTwo = Frame(notebook)
ContainerTwo.pack(fill=BOTH, expand=True)
ContainerThree = Frame(notebook)
ContainerThree.pack(fill=BOTH, expand=True)
ContainerFour = Tkinter.Frame(notebook)
ContainerFour.pack(fill=BOTH, expand=True)

#Create the pages
notebook.add(ContainerOne, text='Mode A')
notebook.add(ContainerTwo, text='Mode B')
notebook.add(ContainerThree, text='Mode C')
notebook.add(ContainerFour, text='Mode D')

canvas = Canvas(ContainerOne, width=200, height=400)
scroll = Scrollbar(ContainerOne, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas = Canvas(ContainerTwo, width=200, height=400)
scroll = Scrollbar(ContainerTwo, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)

canvas = Canvas(ContainerThree, width=200, height=400)
scroll = Scrollbar(ContainerThree, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas = Canvas(ContainerFour, width=200, height=400)
scroll = Scrollbar(ContainerFour, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
frame = Frame(canvas, width=200, height=1000)
canvas.create_window(100, 500, window=frame)

frameOne = None  

def defocus(event):
    event.widget.master.focus_set()    
    if __name__ == '__main__':
        ContainerOne= Tkinter.Label(notebook, text=" 1. Enter Main Details: ", font=("fixedsys", "16","bold italic"))
        frameOne.grid(row=2, columnspan=7, sticky='W', \
                 padx=5, pady=5, ipadx=5, ipady=5)

#Component Selection
componentComb= ttk.Combobox(ContainerOne, width="19")
componentComb = Combobox(ContainerOne, state="readonly", values=("A", "B", "C"))
componentComb.grid(column=4, row=4, columnspan="5", sticky="nswe")
componentComb.set("Main Selection")

root.mainloop()
4

2 回答 2

9

如果您查看Notebook 小部件的选项,您会发现既不存在yview也不yscrollcommand存在。此外,Frame 小部件也不能滚动。

您可以做的是创建一个带有滚动条的 Canvas 小部件frameOne,然后使用create_window.

root = Tk()
root.resizable(0,0)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None
container = Frame(notebook)
container.pack(fill=BOTH, expand=True)
notebook.add(container, text='Mode A')

canvas = Canvas(container, width=200, height=400)
scroll = Scrollbar(container, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)

frame = Frame(canvas, bg='white', width=200, height=1000)
canvas.create_window(100, 500, window=frame)

root.mainloop()
于 2013-04-04T09:18:38.893 回答
4

我已经解决了使用 python 和 tkinter 将表单插入笔记本以及滚动条时遇到的问题。我要感谢@A.Rodas 的帮助。

这是我的代码,希望对你有用!

import Tkinter
from Tkinter import *
from ttk import *
import tkMessageBox
import math 
import ttk 
import Tkinter as tk

def defocus(event):
    event.widget.master.focus_set()

# start of GUI code
root = Tk()
root.title("Model A")
root.resizable(0,0)

# start of Notebook (multiple tabs)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None

# Child frames
ContainerOne = Frame(notebook)
ContainerOne.pack(fill=BOTH, expand=True)
ContainerTwo = Frame(notebook)
ContainerTwo.pack(fill=BOTH, expand=True)

# Create the pages
notebook.add(ContainerOne, text='Mode A')
notebook.add(ContainerTwo, text='Mode B')

canvas1 = Canvas(ContainerOne, width=1200, height=450)
scroll = Scrollbar(ContainerOne, command=canvas1.yview)
canvas1.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas1.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas2 = Canvas(ContainerTwo, width=1200, height=450)
scroll = Scrollbar(ContainerTwo, command=canvas2.yview)
canvas2.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas2.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)

frameOne = Frame(canvas1, width=800, height=450)
canvas1.create_window(250, 125, window=frameOne)

frameTwo = Frame(canvas2, width=800, height=450)
canvas2.create_window(200, 140, window=frameTwo)

# Main Frame

#Close Application Button
def quit(root):
    root.destroy()

ttk.Button(root, text="Close Application", command=lambda root=root:quit(root)).pack()

if __name__ == '__main__':
    #Main Part
    stepOne = Tkinter.LabelFrame(frameOne, text=" 1. Enter Main Details: ", font=("fixedsys", "16","bold italic"))
    stepOne.grid(row=0, columnspan=5, sticky='nsew', padx=5, pady=5, ipadx=5, ipady=5)
    stepTwo = Tkinter.LabelFrame(frameOne, text=" 2. Calculate AP : ", font=("fixedsys", "16","bold italic"))
    stepTwo.grid(row=2, columnspan=7, sticky='WE', padx=5, pady=5, ipadx=5, ipady=5)

# First Step Starts 

# Component Selection
componentComb= ttk.Combobox(stepOne, width="19")
componentComb = Combobox(stepOne, state="readonly", values=("CDA", "VHS", "DVD"))
componentComb.grid(column=4, row=0, columnspan="5", sticky="nswe")
componentComb.set("Component Selection")

# Temperature Selection
tempComb = ttk.Combobox(stepOne, width="12")
tempComb = Combobox(stepOne, state="readonly", values=("-40", "-30", "-20","-10", "0",))
tempComb.grid(column=0, row=2, columnspan="2", sticky="w")
tempComb.set("Temperature Selection")

# Second Step Starts
inEncLbl = Tkinter.Label(stepTwo, text="Oxide:")
inEncLbl.grid(row=2, column=0, sticky='E', padx=5, pady=2)

inEncTxt = Tkinter.Entry(stepTwo, width=6)
inEncTxt.grid(row=2, column=1, sticky='w', pady=2)

outEncLbl = Tkinter.Label(stepTwo, text="Density Rate (DR):")
outEncLbl.grid(row=2, column=5, padx=5, pady=2)

outEncTxt = Tkinter.Entry(stepTwo, width=6)
outEncTxt.grid(row=2, column=7,sticky='w', pady=2)

#End Code
root.mainloop()
于 2013-04-10T10:43:54.477 回答